Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape character \t behaves differently with space

Tags:

python

Why do I see output only if I put space.

print "I love you %s" % "\tI'm tabbled in."
print "I love you %s" % " \tI'm tabbled in."

output

I love you  I'm tabbled in.
I love you      I'm tabbled in.
like image 301
vivekbecks Avatar asked May 06 '15 05:05

vivekbecks


People also ask

How do you escape a special character in a string python?

Escape sequences allow you to include special characters in strings. To do this, simply add a backslash ( \ ) before the character you want to escape.

How do you escape parentheses in Python?

How to escape the parentheses ( and ) in Python regular expressions? Parentheses have a special meaning in Python regular expressions: they open and close matching groups. You can get rid of the special meaning of parentheses by using the backslash prefix: \( and \) .


1 Answers

Typically, \t (TAB) goes to the next tab stop - it is not a synonym for "n spaces".

 I love you XI'm tabbled in.
 I love you  XXXXI'm tabbled in.
 0---1---2---3---4---

The current terminal is configured with a tab stop size of 4 which is shown on the bottom. The "X" are the characters skipped by the tab.

So the first line skips one character with the tab (it goes to tab stop #3) and the second line writes a space and then skips four characters (to get to tab stop #4).

like image 167
user2864740 Avatar answered Nov 14 '22 23:11

user2864740