String common operations
String Manipulation | Meaning Explanation |
---|---|
s=r 'XXX' |
R represents foregoing quoted strings without escaping |
b 、u |
Quoted strings preceding b and u represent byte string and Unicode strings |
s1 + s2 | String splicing |
s * 3 | Repeat string |
s[i]、s[i:j] | String indexing and slicing |
only the (S) | Required length of the string |
"abc % s" % d |
String formatting expressions |
ord() |
Seeking the ASCII-character string |
chr() |
Returns the character corresponding to the ASCII code |
Meaning escape character string list
Escape character | significance |
---|---|
\\ |
\ |
\' |
' |
\" |
" |
\a |
Bell |
\b |
backspace |
\f |
Feed |
\n |
Wrap |
\r |
Enter |
\t |
Horizontal tab |
String common method
find()
Method: String lookup operation. Returns the position of the first substring
s = 'spam'
s.find('pa') #返回1
replace()
Method: replace the contents of the string
#replace用法:replace(old, new, [, count]) 其中count可以省略,表示从左往右替换几个子字符串
s.replace('pa', 'Pa') #s = 'sPam'
split()
Method: The delimiter string into substrings
line = 'aaa, bbb, ccc'
line.split(',') #返回['aaa', 'bbb', 'ccc', 'ddd]
# 若split()内为空则默认以一个或多个空格为分隔符
Rookie Tutorial link: http://www.runoob.com/python/att-string-split.html
strip()
Method: Remove the string t beginning and end of the specified character (the default is a space or a newline)
Derived: lstrip () The rstrip and () method respectively processed from left to right and right to left
Syntax: str.strip ([chars]) #chars: Specifies to delete characters
Rookie Tutorial link: http://www.runoob.com/python/att-string-rstrip.html
upper()
Method: Lowercase letters are converted to upper case string
str.upper() #返回值为小写字母转为大写字母的字符串。
There are similar:
lower()
Convert uppercase letters to lowercase;
capitalize()
The string of letters capitalized rest lowercase
title()
The first letter of each word in a string of letters uppercase lowercase rest
Related Links: blog Garden - Python in upper, lower, capitalize, title
join()
Method: The list of objects and the like can be iterative in particular into a string delimiter
# example:
'.'.join(['a', 'b', 'c']) # 输出:"a.b.c"
Tips: ** applies to only the above-described character string type in python, can be applied to various types of generic operations are in the form of built-in functions or expressions, such as len (x), x [0]; However, the particular type of operation is the form of method calls, such as str.upper ()