Home > Mobile >  How to use "\t" on a variable in Python
How to use "\t" on a variable in Python

Time:01-30

I am teaching myself Python with a book, and, in one of the assignments I need to make a program to strip whitespace before and after a variable. It also says to use the \n and \t escape characters.

I can get strip(), lstrip() and rstrip() to work but \t and \n are giving me trouble.

Is there a way to use \t on a variable?

I tried this:

name = " shane waxwing "
print(\tname)

It only works on strings, like this:

print("\tshane waxwing")

CodePudding user response:

if you are looking for a tab or a newline in front of the variable you could use f-strings:

print(f"\n{variable}")

or, if you prefer you could use string concatenation:

print('\t' variable)

NOTE: this works only if the variable in question is a string else you would need to convert it to a str object before:

print('\t' str(variable))

or

print(''.join("\t",str(variable)))
  •  Tags:  
  • Related