P6-String
P6-String
Lists methods;
String methods;
UTF-16, UTF-32
廖文宏 Python Programming 5
2.2.1 String
Python's strings are immutable sequences.
multiple line string, e.g.
'''
line 1
line 2
'''
String Operators
concatenate(join) +, +=
replicate *, *=
in, not in
chr(code)
takes a code point and returns its character
index step
e.g. word[0:6:2] # 'Pto'
string.count(substring) - 2.2.1.14
string.capitalize(), .upper(), .lower(), .swapcase()
string.center()
string.endswith(substring), string.startswith(substring)
string.find(substring)
find the first occurrence of substring in source string.
2.3.1.17 一覽表
https://docs.python.org/3/library/stdtypes.html#str
廖文宏 Python Programming 13
2.4 String Comparison
just compares code point values, character by character
relation between strings is determined by the first different
character
longer string is considered greater, e.g.
'alpha' < 'alphabet'
upper-case letters are taken as lesser than lower-case, e.g.
'Beta' < 'beta'
'10' < '8' # because '1' < '8'
Do not comparing strings against numbers, e.g.
'10' == 10 # False
'10' > 10 # TypeError Exception
廖文宏 Python Programming 14
2.4.1.3 Sort List containing Strings
sorted()
takes one argument (a list) and returns a new list
list.sort()
Sorting is performed in situ (affects the list itself), no