python字符串详解–str
python中的字符串是不可变序列(sequence)
String literals are written in a variety of ways:
- Single quotes:
'allows embedded "double" quotes'
- Double quotes:
"allows embedded 'single' quotes "
- Triple quoted:
'''Three single quotes,'''"""Three double quotes"""
三引号字符串可以跨多行。特殊字符也会被计入字符串。
如果两个字符串之间只有空格(或没有空格),会被隐含的合并成一个
ss="abc"'DEF' # 'abcDEF'
ss="abc" "abc" # 'abcabc'
ss="""ab
cd
ef g""" 'ab\ncd\nef\tg'
String and Bytes literals
字符串常量?,将其用于字符串引号之前有特殊作用。字符串的默认编码方式是utf-8.
前缀:
- ‘r’,’R’:raw string。将反斜杠看作普通字符。
- ‘b’,’B’:bytes.这产生一个bytes 类型的实例而不是str类型。(存储的是字符的ASCII码),没有ASCII码的字符无效。(0-255)
- ‘u’,’U’:对字符采用unicode码存储。
- ‘f’,’F’:formatted string.对大括号{}中的字符用其值替换。如果要在该字符串中用大括号,则需要用双大括号’‘代替
格式:replacement_field ::= "{" f_expression ["!" conversion] [":" format_spec] "}"
If a conversion is specified, the result of evaluating the expression is converted before formatting. Conversion ‘!s’ calls str() on the result, ‘!r’ calls repr(), and ‘!a’ calls ascii().
Top-level format specifiers may include nested replacement fields. These nested fields may include their own conversion fields and format specifiers, but may not include more deeply-nested replacement fields.
name="Fred"
f"He said his name is {name!r}."
# "He said his name is 'Fred'."
f"He said his name si {repr(name)}." #repr() is equivalent to !r
width=10
precision=4
value=decimal.Decimal("12.34567")
f"result:{value:{width}.{precision}}" #nested fields
# 'result: 12.35'
索引
由于没有单独的character类型,对字符串的索引返回长度为1的字符串
对于非空字符串:s[0]==s[0:1]
String Methods
- capitalize() 返回首字母大写其他字母小写的字符串
- center(width[, fillchar]) 返回将原字符串位于中间两边用指定字符填充的字符串。填充字符默认为空格。
- count(sub[, start[, end]]) 返回非重叠子串的数量。Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
- endswith(suffix[, start[, end]])
- startswith() 返回True 如果字符串以一特定子串结尾。 Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.
- find(sub[, start[, end]])
寻找特定子串并返回子串的起始位置。没找到返回-1。
如果只是需要检查是否子串是否存在用 in 即可
'Py' in 'Python' # True
- format(*args,**kwargs) 见下一节
- index(sub[, start[, end]]) Like find(), but raise ValueError when the substring is not found.
- isalapha() 返回true如果非空字符串的每个字符都是字符
- isdigit() …都是数字
- isalnum() …都是字母或数字
- islower() ..字母都是小写
- isupper() ..字母都是大写
- join(iterable) 如果返回以该字符串连接数组的字符串
- ljust(width[,fillchar]) 左对齐,将字符串右边填一些字符(默认0)以达到指定宽度。
- rjust()
- lower()
- upper()
- lstrip()
- strip([chars]),剥皮,对指定字符串的每一个字符都剥皮
- rstrip()
- replace(old, new[, count]) 对每个旧值用新值代替
- rfind() 返回子串的最大索引
- split(sep=None, maxsplit=-1) 将字符串分割成数组
- title() 返回titlecased version of the string
'Hello world'.title() #'Hello World'
- zfill(width) 左边加0以达到指定宽度
字符串格式化
"First, thou shalt count to {0}" # References first positional argument
"Bring me a {}" # Implicitly references the first positional argument
"From {} to {}" # Same as "From {0} to {1}"
"My quest is {name}" # References keyword argument 'name'
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
#format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]
#fill ::= <any character>
#align ::= "<" | ">" | "=" | "^"
#sign ::= "+" | "-" | " "
#width ::= integer
#grouping_option ::= "_" | ","
#precision ::= integer
#type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
# by position
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
# by name
'abracadabra'
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'
# accessing arguments' attributes
>>> c = 3-5j
>>> ('The complex number {0} is formed from the real part {0.real} '
... 'and the imaginary part {0.imag}.').format(c)
'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
>>> class Point:
... def __init__(self, x, y):
... self.x, self.y = x, y
... def __str__(self):
... return 'Point({self.x}, {self.y})'.format(self=self)
...
>>> str(Point(4, 2))
'Point(4, 2)'
#Accessing arguments’ items:
>>> coord = (3, 5)
>>> 'X: {0[0]}; Y: {0[1]}'.format(coord)
'X: 3; Y: 5'
#Aligning the text and specifying a width:
>>> '{:<30}'.format('left aligned')
'left aligned '
>>> '{:>30}'.format('right aligned')
' right aligned'
>>> '{:^30}'.format('centered')
' centered '
>>> '{:*^30}'.format('centered') # use '*' as a fill char
'***********centered***********'
参考
- python官方文档