Python strings come with a powerful set of processing tools. Python has no distinct type for individual characters; instead you just
use one-character strings.
Strings are derived from sequences, and are immutable. It is easy enough to digest with examples:
S = '' # empty string with single quotes
S ="spam's" # double quotes
S ='s\n\p\ta\x00m' # escape
S=""" ...multipline ..""" # triple-quoted block strings
S=r'\tmp\spam' # Raw strings (no escapes"
S=b'sp\xc4m' # byte strings
S=u'sp\u00c5m' # Unicode
S1+S2 # concatenation
S*3
S[i]
S[i:j]
len(S)
"a %s parrot" % kind # string formatting expression
S.find('pa')
S.rstrip() # remove whitespace
S.replace('pa','xx')
S.split(',')
S.isdigit()
S.lower()
S.endswith('spam')
'spam'.jjoin(strlist) # delimiter join
for x in S: print(x) # iteration
'spam' in S
[c*2 for c in S]
map(ord, S)
re.match('sp(.*)am', line) # pattern matching: library module


雷达卡



京公网安备 11010802022788号







