Home > Enterprise >  Why is the escape sequence \t (tab) not working?
Why is the escape sequence \t (tab) not working?

Time:05-27

this is my code:

print('Yes, my name is\tSwayam')                           # tab
print('Yes, my name is\tSwayam'.expandtabs(tabsize = 11))    

this is the output in terminal:

Yes, my name is Swayam
Yes, my name is       Swayam

software used is 'vs code'

what should I do to make \t work?

I wanted a tab after 'is' in first line which is 4 spaces (i guess) and 11 spaces in second line but it's only 7 spaces

CodePudding user response:

The default tab width is 8. Therefore:

print('Yes, my name is\tSwayam')

...produces...

Yes, my name is Swayam

...because only one space is needed to align with the next tab position (16 in this case)

CodePudding user response:

You can try inserting a space before \t, like this:

print('Yes, my name is \tSwayam') 
print('Yes, my name is\tSwayam')

This is the terminal output:

Yes, my name is         Swayam
Yes, my name is Swayam

Hope this helps you.

  • Related