String is one of Python data types that we've talked about in our previous tutorial, now we're going to learn more about Python string and what things we can do with Python string.
If you don't want the escape character to be interpreted, you can use raw strings by adding "r" before the quotes.
8.1 Basic String
To define Python string we simply just enclose them with single quotes or double quotes. But both of them still need escape character, example>>> 'This is spam' # single quotes
'This is spam'
>>> 'I don\'t want it' # \' to escpae the single quotes
"I don't want it"
>>> "I don't want it" # or just use double quotes
"I don't want it"
>>> "\"I Love you,\" she said." # \" to escape the double quotes
'"I Love you," she said.'
>>> "\"I love you\", she said." # \" to escape the double quotes
'"I love you", she said.'
>>> '"I love you", she said' # or just use single quotes
'"I love you", she said'
>>> '"I\'m talented!", right?'
'"I\'m talented!", right?'
>>> print('"I\'m talented!", right?') # use print() to produce readable output
"I'm talented!", right?
>>> s = 'Line one. \nLine two.' # \n is newline
>>> s # s without print()
'Line one. \nLine two.'
>>> print(s) # with print()
Line one.
Line two.
Escape characters.
Escape Character | Meaning |
---|---|
\newline | Backslash and newline ignored |
\\ | Backslash (\) |
\' | Single quote (') |
\" | Double quote (") |
\a | ASCII Bell (BEL) |
\b | ASCII Backspace (BS) |
\f | ASCII Formfeed (FF) |
\n | ASCII Linefeed (LF) |
\r | ASCII Carriage Return (CR) |
\t | ASCII Horizontal Tab (TAB) |
\v | ASCII Vertical Tab (VT) |
\ooo | Character with octal value ooo |
\xhh | Character with hex value hh |
\N{name} | Character named name in the Unicode database |
\uxxxx | Character with 16-bit hex value xxxx |
\Uxxxxxxxx | Character with 32-bit hex value xxxxxxxx |
>>> print('D:\Students\names')
D:\Students
ames
>>> print(r'D:\Students\names')
D:\Students\names
String literals can span multiple line, you use '''...''' or """...""", end of lines are already included but we can prevent this by using \.
>>> print('''
Just an example
to span string
multiple lines. ''')
Just an example
to span string
multiple lines.
>>> print('''\
I \
Love \
You \
''')
I Love You
Several things that we can do in strings. Here we can concatenate strings with + opreator and repeat them with *.>>> 'Py' + 'thon'
'Python'
>>> ('Py' + 'thon') * 3
'PythonPythonPython'
>>> 'Py' 'thon' # two or more string literals next to each other automatically concatenated
'Python'
>>> pre = 'pre'
>>> pre + 'fix' # concatenate a variable and a literal with '+'
'prefix'
>>> pre + pre + 'fix' ' yeah' # concatenate variables and literals
'preprefix yeah'
Strings can be indexed, start from index 0. Indices may also be negative numbers which start counting from the right.>>> word = 'Example'
>>> word[0] # character in position 0
'E'
>>> word[2] # character in poistion 2
'a'
>>> word[-1] # last character
'e'
>>> word[-2] # second-last character
Besides indexed, Strings can be sliced.
>>> word = 'Example'
>>> word[0:3] # start from 0 to 2, 3 excluded
'Exa'
>>> word[2:4] # start from 2 to 3, 4 excluded
'am'
>>> word[:5] # start from 0 to position 5 (excluded)
'Examp'
>>> word[3:] # start from 4 to the end
'mple'
>>> word[-4:] # start from the fourth-last to the end
'mple'
>>> word[:3] + word[3:]
'Example'
>>> word[:2] + word[2:] # remember start always included, so the end always excluded
'Example'
We can do checking membership operator in Strings, and one thing to remember that strings is immutable for changing the value in specific index.
>>> word = 'Example'
>>> 'w' in word
False
>>> 'x' in word
True
>>> 'z' in word
False
>>> 'a' not in word
False
>>> word[2] = 'e' # this will produce an error, string is immutable
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
word[2] = 'e' # this will producde an error, string is immutable
TypeError: 'str' object does not support item assignment
But you can change the value of defined string in variables
word = "Hello World"
print(word) # Hello World
word = "Hello Python"
print(word) # Hello Python
0 Response to "8. Python Strings"
Post a Comment