Based on the comments on the question here, it appears that the actual confusion stems from how the Python interactive prompt displays the return value of input
. The interactive session always displays values using repr
, which is designed to try to print a string which, when parsed, would produce the original value. For strings this can lead to some confusion, since the thing that is printed in the interactive session is not the actual string, but a representation of the string.
To see the difference, you can try playing around with this program, which will likely be instructive:
#!/usr/bin/env python3
import unicodedata
def main():
while True:
s = input('Enter a string: ')
if not s:
break
print('Got string with length {}'.format(len(s)))
for i, c in enumerate(s):
print('Character {}: {}'.format(i, unicodedata.name(c)))
print('repr() produces: {}'.format(repr(s)))
print('String literally contains: {}'.format(s))
print()
if __name__ == '__main__':
main()
Here are some examples of what it prints:
Enter a string: a
Got string with length 1
Character 0: LATIN SMALL LETTER A
repr() produces: 'a'
String literally contains: a
Enter a string: 'a
Got string with length 2
Character 0: APOSTROPHE
Character 1: LATIN SMALL LETTER A
repr() produces: "'a"
String literally contains: 'a
Enter a string: "a'
Got string with length 3
Character 0: QUOTATION MARK
Character 1: LATIN SMALL LETTER A
Character 2: APOSTROPHE
repr() produces: '"a\''
String literally contains: "a'