.str 字符串类型(一)
1. capitalize 首字母大写
表达式 str.capitalize() ==> str
示例:
1 a = 'alex'
2 v =a.capitalize()3 print(v)
# 输出
# Alex
源码:
1 def capitalize(self, *args, **kwargs): #real signature unknown
2 """
3 Return a capitalized version of the string.4
5 More specifically, make the first character have upper case and the rest lower6 case.7 """
源码
2. casefold 字符串对应字符变小写
表达式 str.casefold() ==> str
示例:
1 a = 'ALEx'
2 v =a.casefold()3 print(v)
# 输出
# alex
源码:
1 def casefold(self, *args, **kwargs): #real signature unknown
2 """Return a version of the string suitable for caseless comparisons."""
源码
3. center 将内容在设置宽度内居中
表达式 str.center(width , "fillchar") ==> str
width 居中宽度
fillchar 填充文本
示例:
1 a = 'alex'
2 v = a.center(20,'-')3 print(v)
# 输出
# --------alex--------
源码:
1 def center(self, *args, **kwargs): #real signature unknown
2 """
3 Return a centered string of length width.4
5 Padding is done using the specified fill character (default is a space).6 """
源码
4. count 去字符串中寻找子序列的总个数
表达式 str.count(sub, start= 0,end=len(string)) ==> int
sub 搜索的字符串
start 计数开始位置序数
end 计数结束位置序数
示例:
1 a = 'alexalexalexalexalexalex'
2 v = a.count('e',2,13)3 print(v)
# 输出
# 3
源码:
1 def count(self, sub, start=None, end=None): #real signature unknown; restored from __doc__
2 """
3 S.count(sub[, start[, end]]) -> int4
5 Return the number of non-overlapping occurrences of substring sub in6 string S[start:end]. Optional arguments start and end are7 interpreted as in slice notation.8 """
9 return 0
源码
5. endswith 判断既定区间中字符串中是否以指定参数结尾,结果输出布尔值
表达式 str.endswith(suffix, start, end) ==> bool
suffix 该参数可以是一个字符串或者是一个元素
start 字符串中的开始位置
end 字符串中的结束位置
示例:
1 a = 'alexalexalexalexalexalexalex'
2 v = a.endswith('x',3,11)3 print(v)
# 输出
# false
源码:
1 def endswith(self, suffix, start=None, end=None): #real signature unknown; restored from __doc__
2 """
3 S.endswith(suffix[, start[, end]]) -> bool4
5 Return True if S ends with the specified suffix, False otherwise.6 With optional start, test S beginning at that position.7 With optional end, stop comparing S at that position.8 suffix can also be a tuple of strings to try.9 """
10 return False
源码
6、startswith 判断既定区间中字符串中是否以指定参数开头,结果输出布尔值
表达式 str.startswith(suffix, start, end) ==> bool
suffix 该参数可以是一个字符串或者是一个元素
start 字符串中的开始位置
end 字符串中的结束位置
示例:
1 a = 'alexalexalexalexalexalexalex'
2 v = a.startswith('x',3,11)3 print(v)
# 输出
# true
源码:
1 def startswith(self, prefix, start=None, end=None): #real signature unknown; restored from __doc__
2 """
3 S.startswith(prefix[, start[, end]]) -> bool4
5 Return True if S starts with the specified prefix, False otherwise.6 With optional start, test S beginning at that position.7 With optional end, stop comparing S at that position.8 prefix can also be a tuple of strings to try.9 """
10 return False
源码
7、find 从前往后在既定右开区间内寻找指定字符串,找不到输出-1。返回的是绝对位值
表达式 str.find(str, beg=0, end=len(string)) ==> int
str 指定检索的字符串
beg 开始索引,默认为0
end 结束索引,默认为字符串的长度
示例:
1 a = 'alexalexalexalexalexalexalex'
2 v = a.find('x',4,11)3 print(v)
# 输出
# 7
源码:
1 def find(self, sub, start=None, end=None): #real signature unknown; restored from __doc__
2 """
3 S.find(sub[, start[, end]]) -> int4
5 Return the lowest index in S where substring sub is found,6 such that sub is contained within S[start:end]. Optional7 arguments start and end are interpreted as in slice notation.8
9 Return -1 on failure.10 """
11 return 0
源码
8、format 将原文字符串中的占位符替换为指定文本,多个替换时替换顺序一一对应
表达式 str.format(*args, **kwargs) ==> str
示例:
1 a = 'i am {} {} {}'
2 v = a.format('jimfrik','age','22')3 print(v)
# 输出
# i am jimfrik age 22
源码:
1 def format(self, *args, **kwargs): #known special case of str.format
2 """
3 S.format(*args, **kwargs) -> str4
5 Return a formatted version of S, using substitutions from args and kwargs.6 The substitutions are identified by braces ('{' and '}').7 """
源码
9、(不懂)format_map 将原文字符串中的占位符替换为指定文本
表达式 str.format_map(mapping) ==> str
源码:
1 def format_map(self, mapping): #real signature unknown; restored from __doc__
2 """
3 S.format_map(mapping) -> str4
5 Return a formatted version of S, using substitutions from mapping.6 The substitutions are identified by braces ('{' and '}').7 """
8 return ""
源码
10、index 从前往后在既定右开区间内寻找指定字符串,找不到报错。返回的是绝对位值
表达式 str.index(sub, start, end) ==> int
sub 搜索的字符串
start 计数开始位置序数
end 计数结束位置序数
示例 :
1 a = 'alexalexalexalexalexalexalex'
2 v = a.index('x',4,11)3 print(v)
# 输出
# 7
源码:
1 def index(self, sub, start=None, end=None): #real signature unknown; restored from __doc__
2 """
3 S.index(sub[, start[, end]]) -> int4
5 Return the lowest index in S where substring sub is found,6 such that sub is contained within S[start:end]. Optional7 arguments start and end are interpreted as in slice notation.8
9 Raises ValueError when the substring is not found.10 """
11 return 0
源码
11、isalnum 判断字符串中是否只包含字母和数字
表达式 str.isalnum() ==> bool
示例:
1 a = 'alex_04327'
2 v =a.isalnum()3 print(v)
# 输出
# false
源码:
1 def isalnum(self, *args, **kwargs): #real signature unknown
2 """
3 Return True if the string is an alpha-numeric string, False otherwise.4
5 A string is alpha-numeric if all characters in the string are alpha-numeric and6 there is at least one character in the string.7 """
源码
12、expandtabs 返回一个字符串的副本。使原字符串中的制表符("\t")的使用空间变大,(“\n”)换行。使用空格来扩展空间。可用来做表格
表达式 str.expandtabs(tabsize=8) ==> str
tabsize 默认空格位为8
示例:
1 a = 'name\tnumber\ttime\njim\t12345\t5:70\nalex\t10987\t6:00\nsmith'
2 v = a.expandtabs(10)3 print(v)
# 输出
# name number time
jim 12345 5:70
alex 10987 6:00
smith
源码:
1 def expandtabs(self, *args, **kwargs): #real signature unknown
2 """
3 Return a copy where all tab characters are expanded using spaces.4
5 If tabsize is not given, a tab size of 8 characters is assumed.6 """
源码