-2

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")
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 1
    What is the original question, and what is your expected output? The first attempt you've given isn't syntactically correct. – BrokenBenchmark Jan 29 '22 at 19:23
  • 1
    "\t" is a common escape character for a "tab" – JonSG Jan 29 '22 at 19:24
  • 1
    can you explain more clearly your question? what do you mean by *"do \t on a variable"*? could you answer these question please?+ – XxJames07- Jan 29 '22 at 19:35
  • @BrokenBenchmark, the first attempt is syntactically incorrect as you said. It wont run. I am trying to get it to apply the \t to the variable 'name'. I'm asking if that's possible. lol – Shane_Waxwing Jan 29 '22 at 19:36
  • 1
    Yes, but I think what everyone is saying is that we don't really understand what your objective is. Is it to print something with an added tab or is it to print something with tabs removed? You kind of seem to be attempting to do both. – JonSG Jan 29 '22 at 19:37
  • 1
    @JonSG, I want to print a variable with an added tab – Shane_Waxwing Jan 29 '22 at 19:38
  • So you're looking for a code snippet that strips whitespace from both ends of the string, and then prints it with a `\t` prepended? – BrokenBenchmark Jan 29 '22 at 19:39
  • Please update your question with this additional information and check out this duplicate as it will have lots of answers in it: https://stackoverflow.com/questions/4488570/how-do-i-write-a-tab-in-python#:~:text=It's%20usually%20%5Ct%20in%20command,%2D%3E%20hello%2D%2D%2D%3Ealex%20. – JonSG Jan 29 '22 at 19:41

2 Answers2

1

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)))
XxJames07-
  • 1,833
  • 1
  • 4
  • 17
  • The strip() method is a part of the assignment, but its a part I understand and can get to work. I was able to get rid of white spaces but I couldn't figure out how to apply \t to a variable. I probably should have left that bit out. Definitely my mistake. – Shane_Waxwing Jan 29 '22 at 19:46
0

\t is used to provide a whitespace equal to a tab. for example:

Sample String : "Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are"

Output:

Twinkle, twinkle, little star,
How I wonder what you are! 
    Up above the world so high,         
    Like a diamond in the sky. 

Twinkle, twinkle, little star, How I wonder what you are

Solution:

print("Twinkle, twinkle, little star,\n\tHow I wonder what you are!\n\t\tUp above the world so high, \n\t\tLike a diamond in the sky. \nTwinkle, twinkle, little star,\n\tHow I wonder what you are")