Python字符串方法?
Python提供了许多可用于字符串的内置方法。
以下是Python3中可用的字符串方法的列表。
>>> mystring = "hello python" >>> print(mystring.capitalize()) Hello python
>>> mystring = "hello PYTHON" >>> print(mystring.casefold()) hello python
>>> mystring = "Hello" >>> x = mystring.center(12, "-") >>> print(x) ---Hello----
>>> mystr = "Hello Python"
>>> print(mystr.count("o"))
2
>>> print(mystr.count("th"))
1
>>> print(mystr.count("l"))
2
>>> print(mystr.count("h"))
1
>>> print(mystr.count("H"))
1
>>> print(mystr.count("hH"))
0严格(编码错误会引发UnicodeError)
忽视
更换
xmlcharrefreplace
反斜杠替换
通过codecs.register_error()注册的任何其他名称
>>> mystr = 'python!'
>>> print('The string is:',
mystr)
The string is: python!
>>> print('The encoded
version is: ',
mystr.encode("ascii",
"ignore"))
The encoded version is:
b'python!'
>>> print('The encoded
version (with replace) is:',
mystr.encode("ascii",
"replace"))
The encoded version (with
replace) is: b'python!'>>> mystr = "Python"
>>>
print(mystr.endswith("y"))
False
>>>
print(mystr.endswith("hon"))
True>>> mystr = "1\t2\t3" >>> print(mystr) 1 2 3 >>> print(mystr.expandtabs()) 1 2 3 >>> print(mystr.expandtabs(tabsi ze=15)) 1 2 3 >>> print(mystr.expandtabs(tabsi ze=2)) 1 2 3
>>> mystring = "Python"
>>>
print(mystring.find("P"))
0
>>>
print(mystring.find("on"))
4>>> print("{} and
{}".format("Apple",
"Banana"))
Apple and Banana
>>> print("{1} and
{0}".format("Apple",
"Banana"))
Banana and Apple
>>> print("{lunch} and
{dinner}".format(lunch="Peas
", dinner="Beans"))
Peas and Beans>>> lunch = {"Food":
"Pizza", "Drink": "Wine"}
>>> print("Lunch: {Food},
{Drink}".format_map(lunch))
Lunch: Pizza, Wine
>>> class Default(dict):
def __missing__(self,
key):
return key
>>> lunch = {"Drink":
"Wine"}
>>> print("Lunch: {Food},
{Drink}".format_map(Default(
lunch)))
Lunch: Food, Wine>>> mystr = "HelloPython"
>>> print(mystr.index("P"))
5
>>>
print(mystr.index("hon"))
8
>>> print(mystr.index("o"))
4>>> mystr = "HelloPython" >>> print(mystr.isalnum()) True >>> a = "123" >>> print(a.isalnum()) True >>> a= "$*%!!!" >>> print(a.isalnum()) False
>>> mystr = "HelloPython" >>> print(mystr.isalpha()) True >>> a = "123" >>> print(a.isalpha()) False >>> a= "$*%!!!" >>> print(a.isalpha()) False
>>> mystr = "HelloPython" >>> print(mystr.isdecimal()) False >>> a="1.23" >>> print(a.isdecimal()) False >>> c = u"\u00B2" >>> print(c.isdecimal()) False >>> c="133" >>> print(c.isdecimal()) True
>>> c="133" >>> print(c.isdigit()) True >>> c = u"\u00B2" >>> print(c.isdigit()) True >>> a="1.23" >>> print(a.isdigit()) False
>>> c="133" >>> print(c.isidentifier()) False >>> c="_user_123" >>> print(c.isidentifier()) True >>> c="Python" >>> print(c.isidentifier()) True
>>> c="Python" >>> print(c.islower()) False >>> c="_user_123" >>> print(c.islower()) True >>> print(c.islower()) False
>>> c="133" >>> print(c.isnumeric()) True >>> c="_user_123" >>> print(c.isnumeric()) False >>> c="Python" >>> print(c.isnumeric()) False
>>> c="133" >>> print(c.isprintable()) True >>> c="_user_123" >>> print(c.isprintable()) True >>> c="\t" >>> print(c.isprintable()) False
>>> c="133" >>> print(c.isspace()) False >>> c="Hello Python" >>> print(c.isspace()) False 73 >>> c="Hello" >>> print(c.isspace()) False >>> c="\t" >>> print(c.isspace()) True
>>> c="133" >>> print(c.istitle()) False >>> c="Python" >>> print(c.istitle()) True >>> c="\t" >>> print(c.istitle()) False
>>> c="Python" >>> print(c.isupper()) False >>> c="PYHTON" >>> print(c.isupper()) True >>> c="\t" >>> print(c.isupper()) False
>>> a ="-"
>>> print(a.join("123"))
1-2-3
>>> a="Hello Python"
>>> a="**"
>>> print(a.join("Hello
Python"))
H**e**l**l**o**
**P**y**t**h**o**n>>> a="Hello" >>> b = a.ljust(12, "_") >>> print(b) Hello_______
>>> a = "Python" >>> print(a.lower()) Python
>>> a = " Hello " >>> print(a.lstrip(), "!") Hello
>>> frm = "SecretCode" >>> to = "4203040540" >>> trans_table = str.maketrans(frm,to) >>> sec_code = "Secret Code".translate(trans_table) >>> print(sec_code) 400304 0540
>>> mystr = "Hello-Python"
>>> print(mystr.partition("-
"))
('Hello', '-', 'Python')
74
>>>
print(mystr.partition("."))
('Hello-Python', '', '')>>> mystr = "Hello Python.
Hello Java. Hello C++."
>>>
print(mystr.replace("Hello",
"Bye"))
Bye Python. Bye Java. Bye
C++.
>>>
print(mystr.replace("Hello",
"Hell", 2))
Hell Python. Hell Java.
Hello C++.>>> mystr = "Hello-Python"
>>> print(mystr.rfind("P"))
6
>>> print(mystr.rfind("-"))
5
>>> print(mystr.rfind("z"))
-1>>> mystr = "Hello-Python"
>>> print(mystr.rindex("P"))
6
>>> print(mystr.rindex("-"))
5
>>> print(mystr.rindex("z"))
Traceback (most recent call
last):
File "<pyshell#253>", line
1, in <module>
print(mystr.rindex("z"))
ValueError: substring not
found>>> mystr = "Hello Python" >>> mystr1 = mystr.rjust(20, "-") >>> print(mystr1) --------Hello Python
>>> mystr = "Hello Python"
>>>
print(mystr.rpartition("."))
('', '', 'Hello Python')
>>> print(mystr.rpartition("
"))
('Hello', ' ', 'Python')>>> mystr = "Hello Python" >>> print(mystr.rsplit()) ['Hello', 'Python'] >>> mystr = "Hello-Python- Hello" >>> print(mystr.rsplit(sep="-", maxsplit=1)) ['Hello-Python', 'Hello']
>>> mystr = "Hello Python" >>> print(mystr.rstrip(), "!") Hello Python ! >>> mystr = "------------ Hello Python-----------" >>> print(mystr.rstrip(), "- ") ------------Hello Python---- ------- - >>> print(mystr.rstrip(), "_") ------------Hello Python---- ------- _
>>> mystr = "Hello Python"
>>> print(mystr.split())
['Hello', 'Python']
>>> mystr1="Hello,,Python"
>>> print(mystr1.split(","))
['Hello', '', 'Python']>>> mystr = "Hello:\n\n Python\r\nJava\nC++\n" >>> print(mystr.splitlines()) ['Hello:', '', ' Python', 'Java', 'C++'] >>> print(mystr.splitlines(keepe nds=True)) ['Hello:\n', '\n', ' Python\r\n', 'Java\n', 'C++\n']
>>> mystr = "Hello Python"
>>>
print(mystr.startswith("P"))
False
>>>
print(mystr.startswith("H"))
True
>>>
print(mystr.startswith("Hell
"))
True>>> mystr = " Hello Python " >>> print(mystr.strip(), "!") Hello Python ! >>> print(mystr.strip(), " ") Hello Python
>>> mystr = "Hello PYthon" >>> print(mystr.swapcase()) hELLO python
>>> mystr = "Hello PYthon" >>> print(mystr.title()) Hello Python >>> mystr = "HELLO JAVA" >>> print(mystr.title()) Hello Java
>>> frm = "helloPython" >>> to = "40250666333" >>> trans_table = str.maketrans(frm, to) >>> secret_code = "Secret Code".translate(trans_table) >>> print(secret_code) S0cr06 C3d0
>>> mystr = "hello Python" >>> print(mystr.upper()) HELLO PYTHON
>>> mystr = "999" >>> print(mystr.zfill(9)) 000000999 >>> mystr = "-40" >>> print(mystr.zfill(5)) -0040