赛博红兔的科技博客

CyberHongTu shares news, insights, and musings on fascinating technology subjects.


和我一起玩Python:7. 字符串的方法

上一集里咱们详细聊了聊字符串的那些事儿,但还有些字符串的方法没提到呢。用dir()这个函数能列出一个字符串的所有方法,比如说,咱有个变量course,值是”Learn Python With Me”。咱聊到了几个方法,像是format()用来格式化,upper()能把字符串全变成大写,lower()则是变小写,find()用来找字符串里特定的字符,这玩意儿是区分大小写的,replace()可以把字符串的一部分替换成别的字符串,in或者not in用来检查某个元素在不在字符串里,返回个布尔值,count()则是数一数某个元素在字符串里出现了几次。

string Method List

MethodDescription
capitalize()Converts the first character to upper case
casefold()Converts string into lower case
center()Returns a centered string
count()Returns the number of times a specified value occurs in a string
encode()Returns an encoded version of the string
endswith()Returns true if the string ends with the specified value
expandtabs()Sets the tab size of the string
find()Searches the string for a specified value and returns the position of where it was found
format()Formats specified values in a string
format_map()Formats specified values in a string
index()Searches the string for a specified value and returns the position of where it was found
isalnum()Returns True if all characters in the string are alphanumeric
isalpha()Returns True if all characters in the string are in the alphabet
isascii()Returns True if all characters in the string are ascii characters
isdecimal()Returns True if all characters in the string are decimals
isdigit()Returns True if all characters in the string are digits
isidentifier()Returns True if the string is an identifier
islower()Returns True if all characters in the string are lower case
isnumeric()Returns True if all characters in the string are numeric
isprintable()Returns True if all characters in the string are printable
isspace()Returns True if all characters in the string are whitespaces
istitle()Returns True if the string follows the rules of a title
isupper()Returns True if all characters in the string are upper case
join()Converts the elements of an iterable into a string
ljust()Returns a left justified version of the string
lower()Converts a string into lower case
lstrip()Returns a left trim version of the string
maketrans()Returns a translation table to be used in translations
partition()Returns a tuple where the string is parted into three parts
replace()Returns a string where a specified value is replaced with a specified value
rfind()Searches the string for a specified value and returns the last position of where it was found
rindex()Searches the string for a specified value and returns the last position of where it was found
rjust()Returns a right justified version of the string
rpartition()Returns a tuple where the string is parted into three parts
rsplit()Splits the string at the specified separator, and returns a list
rstrip()Returns a right trim version of the string
split()Splits the string at the specified separator, and returns a list
splitlines()Splits the string at line breaks and returns a list
startswith()Returns true if the string starts with the specified value
strip()Returns a trimmed version of the string
swapcase()Swaps cases, lower case becomes upper case and vice versa
title()Converts the first character of each word to upper case
translate()Returns a translated string
upper()Converts a string into upper case
zfill()Fills the string with a specified number of 0 values at the beginning

字符串方法列表

方法描述
capitalize()将字符串的第一个字母转换为大写
casefold()将字符串转换为小写
center()返回居中的字符串
count()返回指定值在字符串中出现的次数
encode()返回字符串的编码版本
endswith()如果字符串以指定的值结尾,则返回True
expandtabs()设置字符串的制表符大小
find()搜索字符串中的指定值,并返回其所在位置
format()格式化字符串中指定的值
format_map()格式化字符串中指定的值
index()搜索字符串中的指定值,并返回其所在位置
isalnum()如果字符串中的所有字符都是字母数字,则返回True
isalpha()如果字符串中的所有字符都在字母表中,则返回True
isascii()如果字符串中的所有字符都是ASCII字符,则返回True
isdecimal()如果字符串中的所有字符都是十进制,则返回True
isdigit()如果字符串中的所有字符都是数字,则返回True
isidentifier()如果字符串是标识符,则返回True
islower()如果字符串中的所有字符都是
isnumeric()如果字符串中的所有字符都是数字,则返回 True
isprintable()如果字符串中的所有字符都是可打印的,则返回 True
isspace()如果字符串中的所有字符都是空格,则返回 True
istitle()如果字符串遵循标题的规则,则返回 True
isupper()如果字符串中的所有字符都是大写字母,则返回 True
join()将可迭代对象的元素转换为字符串
ljust()返回字符串的左对齐版本
lower()将字符串转换为小写
lstrip()返回字符串的左边去空格版本
maketrans()返回用于翻译的翻译表
partition()返回一个元组,其中字符串被分成三部分
replace()返回指定值被指定值替换的字符串
rfind()搜索字符串中的指定值,并返回最后一个找到的位置
rindex()搜索字符串中的指定值,并返回最后一个找到的位置
rjust()返回字符串的右对齐版本
rpartition()返回一个元组,其中字符串被分成三部分
rsplit()将字符串按指定分隔符分割,并返回一个列表
rstrip()返回字符串的右侧修剪版本
split()将字符串按指定分隔符分割,并返回一个列表
splitlines()在换行符处分割字符串,并返回一个列表
startswith()如果字符串以指定的值开头,则返回true
strip()返回字符串的修剪版本
swapcase()交换大小写,小写变成大写,大写变成小写
title()将每个单词的首字母转换为大写
translate()返回翻译后的字符串
upper()将字符串转换为大写
zfill()在开头填充指定数量的0值


Leave a comment