找回密码
快捷导航
使用引号或双引号来创建字符串
访问子字符串,可以使用方括号来截取字符串


[Python] 纯文本查看 复制代码
print(dir(str))
methods = ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',
           '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',
           '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__',
           '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__',
           '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold',
           'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map',
           'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower',
           'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower',
           'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
           'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',
           'translate', 'upper', 'zfill']


[Python] 纯文本查看 复制代码
# capitalize() 将第一个字母大写
'''
capitalize(...)
    S.capitalize() -> str

    Return a capitalized version of S, i.e. make the first character
    have upper case and the rest lower case.
'''
#print(help(str.capitalize))
a = 'asdffghjkl'
b = a.capitalize()
print(b)                            # b = Asdffghjkl


[Python] 纯文本查看 复制代码
# casefold() 返回小写,与lower()类似,但lower只针对ASCII,casefold()针对Unicode()
'''
casefold(...)
    S.casefold() -> str

    Return a version of S suitable for caseless comparisons.
'''
print(help(str.casefold))
a = 'ASD'
b = a.casefold()
print(b)                                    # b = asd


[Python] 纯文本查看 复制代码
# 用指定的宽度来返回一个居中版的s,
# 如果需要的话,就用fillchar进行填充,默认是空格。但是不会对s进行截取。
# 即如果s的长度比width大,也不会对s进行截取。
'''
center(...)
    S.center(width[, fillchar]) -> str

    Return S centered in a string of length width. Padding is
    done using the specified fill character (default is a space)
'''
print(help(str.center))
a = 'asdfghjkl'
b = a.center(20, '-')               # 长度为20,a字符串居中,左右用数字5填充
print(b)                            # b = 55555asdfghjkl555555


[Python] 纯文本查看 复制代码
'''
count(...)
    S.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are
    interpreted as in slice notation.
'''
print(help(str.count))
a = 'asdfsghjkl'
b = a.count('s', 1, 2)               # 从下标为1到5计数‘s’的个数
print(b)                             # b = 2


[Python] 纯文本查看 复制代码
# 以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。
'''
encode(...)
    S.encode(encoding='utf-8', errors='strict') -> bytes

    Encode S using the codec registered for encoding. Default encoding
    is 'utf-8'. errors may be given to set a different error
    handling scheme. Default is 'strict' meaning that encoding errors raise
    a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
    'xmlcharrefreplace' as well as any other name registered with
    codecs.register_error that can handle UnicodeEncodeErrors.
'''
print(help(str.encode))
a = 'asdfsghjkl'
b = a.encode('utf-8')
print(b)                             # b = b'asdfsghjkl'


[Python] 纯文本查看 复制代码
# 判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。
# 可选参数"start"与"end"为检索字符串的开始与结束位置。

'''
endswith(...)
    S.endswith(suffix[, start[, end]]) -> bool

    suffix -- 该参数可以是一个字符串或者是一个元素。
    start -- 字符串中的开始位置。
    end -- 字符中结束位置。

    Return True if S ends with the specified suffix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    suffix can also be a tuple of strings to try.
'''
print(help(str.endswith))
a = 'asdfsghjk'
b = a.endswith('k')
c = a.endswith('j')
d = a.endswith('a', 0, 1)
e = a.endswith('a', 0, 2)
print(b)                             # b = True
print(c)                             # c = False
print(d)                             # d = True
print(e)                             # e = False


[Python] 纯文本查看 复制代码
# 判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。
# 可选参数"start"与"end"为检索字符串的开始与结束位置。

'''
expandtabs(...)
    S.expandtabs(tabsize=8) -> str

    Return a copy of S where all tab characters are expanded using spaces.
    If tabsize is not given, a tab size of 8 characters is assumed.
'''
print(help(str.expandtabs))
a = 'asdfsghjk'
b = a.endswith('k')
c = a.endswith('j')
d = a.endswith('a', 0, 1)
e = a.endswith('a', 0, 2)
print(b)                             # b = True
print(c)                             # c = False
print(d)                             # d = True
print(e)                             # e = False


[Python] 纯文本查看 复制代码
'''
find(...)
    检测字符串中是否包含子字符串 str ,
    如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,
    如果包含子字符串,则返回开始的索引值,否则返回-1。

    S.find(sub[, start[, end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.
'''
print(help(str.find))
a = 'asdfsghjk'
b = a.find('d')
c = a.find('f', 1, 4)               #
d = a.find('f', 1, 3)
print(b)                            # 2 下标
print(c)                            # 2 从第一个字符到第三个字符中找,找到了则返回下标
print(d)                            # -1


[Python] 纯文本查看 复制代码
'''
format(...)
    # 格式换字符串输出(方法与%相似,但可以指定顺序)
    S.format(*args, **kwargs) -> str

    Return a formatted version of S, using substitutions from args and kwargs.
    The substitutions are identified by braces ('{' and '}').

None
'''
print(help(str.format))
name = 'StivenWang'
fruit = 'apple'
a = 'my name is {},I like {}'.format(name,fruit)
b = 'my name is {1},I like {0}'.format(fruit,name)
c = 'my name is {mingzi},I like {shuiguo}'.format(shuiguo=fruit,mingzi=name)
print(a)                        # my name is StivenWang,I like apple
print(b)                        # my name is StivenWang,I like apple
print(c)                        # my name is StivenWang,I like apple


[Python] 纯文本查看 复制代码
'''
index(...)
    检测字符串string中是否包含子字符串 str ,
    如果存在,则返回str在string中的索引值,
    如果指定beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,
    该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常(ValueError: substring not found)。
    S.index(sub[, start[, end]]) -> int

    Like S.find() but raise ValueError when the substring is not found.

None
'''
print(help(str.index))
a = "this is string example....wow!!!"
b = "exam"
c = a.index(b)
d = a.index(b, 20)
print(c)                            # 15  返回b字符串的第一个字符的索引值
print(d)                            # 报错


[Python] 纯文本查看 复制代码
'''
isalnum(...)
    检测字符串是否由字母或数字组成
    S.isalnum() -> bool

    Return True if all characters in S are alphanumeric
    and there is at least one character in S, False otherwise.

None
'''
print(help(str.isalnum))
a = "thisisstringexamplewow"
b = "123"
c = 'asdf1234'
d = 'asdf!@@#'
e = 'a = "this is string example wow"'
print(a.isalnum())                  # True  全部由字母构成
print(b.isalnum())                  # True  全部由数字构成
print(c.isalnum())                  # True  全部由字母或者数字构成
print(d.isalnum())                  # False 有特殊字符
print(e.isalnum())                  # False 有空格


[Python] 纯文本查看 复制代码
'''
isalpha(...)
    检测字符串是否只由字母组成
    S.isalpha() -> bool

    Return True if all characters in S are alphabetic
    and there is at least one character in S, False otherwise.
None
'''
print(help(str.isalpha))
a = "thisisstringexamplewow"
b = "123"
c = 'asdf1234'
d = 'asdf!@@#'
e = 'a = "this is string example wow"'
print(a.isalpha())                  # True   全部由字母构成
print(b.isalpha())                  # False  有数字
print(c.isalpha())                  # False  有数字
print(d.isalpha())                  # False  没有字母
print(e.isalpha())                  # False  有空格


[Python] 纯文本查看 复制代码
'''
isdigit()
True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
False: 汉字数字
Error: 无

isdecimal()
True: Unicode数字,,全角数字(双字节)
False: 罗马数字,汉字数字
Error: byte数字(单字节)

isnumeric()
True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
False: 无
Error: byte数字(单字节)
'''
num = "1"  #unicode
a = num.isdigit()   # True
b = num.isdecimal() # True
c = num.isnumeric() # True
print(a, b, c)

num = "1" # 全角
d = num.isdigit()   # True
e = num.isdecimal() # True
f = num.isnumeric() # True
print(d, e, f)

# num = b"1" # byte
# g = num.isdigit()   # True
# h = num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
# i = num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'
# print(g, h, i)

num = "IV" # 罗马数字
j = num.isdigit()   # True
k = num.isdecimal() # False
l = num.isnumeric() # True
print(j, k, l)

num = "四" # 汉字
m = num.isdigit()   # False
n = num.isdecimal() # False
o = num.isnumeric() # True
print(m, n, o)


[Python] 纯文本查看 复制代码
'''
isidentifier(...)
    判断字符串是否是合法的标识符,字符串仅包含中文字符合法,实际上这里判断的是变量名是否合法
    S.isidentifier() -> bool

    Return True if S is a valid identifier according
    to the language definition.

    Use keyword.iskeyword() to test for reserved identifiers
    such as "def" and "class".
'''
print(help(str.isidentifier))
a = "thisisstringexamplewow"
b = "123"
c = 'asdf1234'
d = 'asdf!@@#'
e = 'a = "this is string example wow"'
print(a.isidentifier())                  # True   全部由字母构成
print(b.isidentifier())                  # False  不能以数字开头
print(c.isidentifier())                  # True   字母开头合法
print(d.isidentifier())                  # False  有特殊字符(不能有除了字母数字下划线的其他字符)
print(e.isidentifier())                  # False  有空格(不能有除了字母数字下划线的其他字符)


[Python] 纯文本查看 复制代码
'''
islower(...)
    检测字符串是否由小写字母组成
    S.islower() -> bool

    Return True if all cased characters in S are lowercase and there is
    at least one cased character in S, False otherwise.

None
'''
print(help(str.islower))
a = "Thisisstringexamplewow"
c = 'Ssdf1234'
d = 'asdf!@@#'
e = 'a = "this is string example wow"'
print(a.islower())                  # False   有大写字母
print(c.islower())                  # False   有大写字母
print(d.islower())                  # True    全部小写
print(e.islower())                  # True    全部小写


[Python] 纯文本查看 复制代码
'''
isprintable(...)
    判断字符串所包含的字符是否全部可打印。字符串包含不可打印字符,如转义字符,将返回False。
    S.isprintable() -> bool

    Return True if all characters in S are considered
    printable in repr() or S is empty, False otherwise.
'''
print(help(str.isprintable))
a = "Thisisstringexamplewow"
c = 'Ssdf1234\t'
d = 'asdf!@@#\n'
e = 'a = "this is string example wow"\a'
print(a.isprintable())                  # True
print(c.isprintable())                  # False   有制表符
print(d.isprintable())                  # False   有换行符
print(e.isprintable())                  # False


[Python] 纯文本查看 复制代码
'''
isspace(...)
    S.isspace() -> bool
    判断字符串是否仅包含空格或制表符。注意:空格字符与空白是不同的
    Return True if all characters in S are whitespace
    and there is at least one character in S, False otherwise.
'''
print(help(str.isspace))
a = ""
c = 'Ssdf1234\t'
d = ' '
e = '\t'
print(a.isspace())                  # False   空白不算空格字符
print(c.isspace())                  # False   不仅仅只有制表符,还有其他字符
print(d.isspace())                  # True    空格字符
print(e.isspace())                  # True    制表符


[Python] 纯文本查看 复制代码
'''
istitle(...)
    检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写
    S.istitle() -> bool

    Return True if S is a titlecased string and there is at least one
    character in S, i.e. upper- and titlecase characters may only
    follow uncased characters and lowercase characters only cased ones.
    Return False otherwise.
'''
print(help(str.istitle))
a = "Thisisstringexamplewow"
b = "ThisisstRingexamplewow"
c = 'ssdf1234'
print(a.istitle())                  # True   首字符大写,其他字符全小写
print(b.istitle())                  # False  首字符大写,其他字符并不是全小写
print(c.istitle())                  # False  首字符不是大写


[Python] 纯文本查看 复制代码
'''
isupper(...)
    检测字符串中所有的字母是否都为大写
    S.isupper() -> bool

    Return True if all cased characters in S are uppercase and there is
    at least one cased character in S, False otherwise.
'''
print(help(str.isupper))
a = "Thisisstringexamplewow"
b = "ThisisstRingexamplewow"
c = 'ASD'
print(a.isupper())                  # False   有小写字符
print(b.isupper())                  # False   有小写字符
print(c.isupper())                  # True    全部大写


[Python] 纯文本查看 复制代码
'''
join(...)
    用于将序列中的元素以指定的字符连接生成一个新的字符串。
    S.join(iterable) -> str

    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.
'''
print(help(str.join))
a = "Thisisstringexamplewow"
b = "-"
c = b.join(a)
print(c)                  # T-h-i-s-i-s-s-t-r-i-n-g-e-x-a-m-p-l-e-w-o-w
z = 'aaa'
x = ['b','b','b']
c = '&'.join([z, x[1:]])
print(c)


[Python] 纯文本查看 复制代码
'''
ljust(...)
    返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。
    如果指定的长度小于原字符串的长度则返回原字符串。
    S.ljust(width[, fillchar]) -> str

    Return S left-justified in a Unicode string of length width. Padding is
    done using the specified fill character (default is a space).
'''
print(help(str.ljust))
a = "asdf"
print(a.ljust(8))                  # asdf
print(a.ljust(8, '+'))             # asdf++++


[Python] 纯文本查看 复制代码
'''
lower(...)
    转换字符串中所有大写字符为小写。
    S.lower() -> str

    Return a copy of the string S converted to lowercase.
'''
print(help(str.lower))
a = "ASDfgh"
print(a.lower())                  # asdfgh


[Python] 纯文本查看 复制代码
'''
lstrip(...)
    用于截掉字符串左边的空格或指定字符
    S.lstrip([chars]) -> str

    Return a copy of the string S with leading whitespace removed.
    If chars is given and not None, remove characters in chars instead.
'''
print(help(str.lstrip))
a = "***ASDfgh"
b = '***ASDfgh'
c = '   asdfsadf'
print(a.lstrip())                  # ***ASDfgh
print(b.lstrip('*'))               # ASDfgh
print(c.lstrip())                  # asdfsadf


[Python] 纯文本查看 复制代码
'''
maketrans(x, y=None, z=None, /)
    Return a translation table usable for str.translate().

    If there is only one argument, it must be a dictionary mapping Unicode
    ordinals (integers) or characters to Unicode ordinals, strings or None.
    Character keys will be then converted to ordinals.
    If there are two arguments, they must be strings of equal length, and
    in the resulting dictionary, each character in x will be mapped to the
    character at the same position in y. If there is a third argument, it
    must be a string, whose characters will be mapped to None in the result.
'''
print(help(str.maketrans))
from string import maketrans
suchas = maketrans('sm','@$')
s = 'this is sam\'s dog'
print(s)
"this is sam's dog"
print(s.translate(suchas))
"thi@ i@ @a$'@ dog"
print(s.translate(suchas,"dog")) #去除d,o,g字符
"thi@ i@ @a$'@ "


[Python] 纯文本查看 复制代码
'''
partition(...)
    根据指定的分隔符将字符串进行分割
    (返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串)
    S.partition(sep) -> (head, sep, tail)

    Search for the separator sep in S, and return the part before it,
    the separator itself, and the part after it.  If the separator is not
    found, return S and two empty strings.
'''
print(help(str.partition))
a = "ASDfgh*123456"
print(a.partition('*'))                  # ('ASDfgh', '*', '123456')


[Python] 纯文本查看 复制代码
'''
replace(...)
    把字符串中的 old(旧字符串)替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
    S.replace(old, new[, count]) -> str

    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.
'''
print(help(str.replace))
a = "ASDfgh*123456ASDfgh*123456ASDfgh*123456ASDfgh*123456"
print(a.replace('*', '-'))                  # ASDfgh-123456ASDfgh-123456ASDfgh-123456ASDfgh-123456
print(a.replace('*', '-', 2))               # ASDfgh-123456ASDfgh-123456ASDfgh*123456ASDfgh*123456
print(a)


[Python] 纯文本查看 复制代码
'''
rfind(...)
    返回字符串最后一次出现的位置,如果没有匹配项则返回-1
    S.rfind(sub[, start[, end]]) -> int

    Return the highest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.
'''
print(help(str.rfind))
a = "ASDfgh*123456ASDfgh*123456ASDfgh*123456ASDfgh*123456"
print(a.rfind('*'))                     # 45
print(a.rfind('*', 0, 7))               # 6
print(a.rfind('*', 0, 6))               # -1


[Python] 纯文本查看 复制代码
'''
rindex(...)
    返回子字符串 str 在字符串中最后出现的位置,
    如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间
    S.rindex(sub[, start[, end]]) -> int

    Like S.rfind() but raise ValueError when the substring is not found.
'''
print(help(str.rindex))
a = "ASDfgh*123456ASDfgh*123456ASDfgh*123456ASDfgh*123456"
print(a.rindex('*'))                     # 45
print(a.rindex('*', 0, 7))               # 6
print(a.rindex('*', 0, 6))               # 报错


[Python] 纯文本查看 复制代码
'''
rjust(...)
    返回的原/新字符串右对齐,且默认使用空格填充至指定长度(width)的新字符串。
    如果指定的长度(width)小于原字符串的长度则返回原字符串
    S.rjust(width[, fillchar]) -> str

    Return S right-justified in a string of length width. Padding is
    done using the specified fill character (default is a space).
'''
print(help(str.rjust))
a = "ASDfgh*123456"
print(a.rjust(20))                     #        ASDfgh*123456
print(a.rjust(20, '-'))                # -------ASDfgh*123456


[Python] 纯文本查看 复制代码
'''
rpartition(...)
    与str.partition()相反,从原字符串的最右边开始拆分,
    但是同样返回包含三个元素的元组:倒数第一个Sep之前的字符串,Sep字符,Sep之后的字符串。
    注意”倒数Sep之前的字符串”,这个之前的字符串,是从原字符串的最左边开始算,并不是最右边。
    S.rpartition(sep) -> (head, sep, tail)

    Search for the separator sep in S, starting at the end of S, and return
    the part before it, the separator itself, and the part after it.  If the
    separator is not found, return two empty strings and S.
'''
print(help(str.rpartition))
a = "abccdee"
print(a.rpartition('c'))                     # ('abc', 'c', 'dee')
print(a.rpartition('f'))                     # ('', '', 'abccdee')


[Python] 纯文本查看 复制代码
'''
rsplit(...)
    与str.split()类似,只是它从最右边开始拆分。只有在指定maxsplit的情况下才会看到效果。
    S.rsplit(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string, starting at the end of the string and
    working to the front.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified, any whitespace string
    is a separator.
'''
print(help(str.rsplit))
a = "abcdeabcdeabcdeabcdeabcdeabcde"
print(a.rsplit('c'))                        # 不指定maxsplit,返回的结果与str.split()相同['ab', 'deab', 'deab', 'deab', 'deab', 'deab', 'de']
print(a.rsplit('c', 1))                     # 从右开始拆,只拆一次 ['abcdeabcdeabcdeabcdeabcdeab', 'de']
print(a.rsplit('c', 3))                     # 从右开始拆,拆三次   ['abcdeabcdeabcdeab', 'deab', 'deab', 'de']


[Python] 纯文本查看 复制代码
'''
split(...)
    通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔 num 个子字符串
    S.split(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.
'''
print(help(str.split))
a = "abcdeabcdeabcdeabcdeabcdeabcde"
print(a.split('c'))                        # ['ab', 'deab', 'deab', 'deab', 'deab', 'deab', 'de']
print(a.split('c', 1))                     # ['ab', 'deabcdeabcdeabcdeabcdeabcde']
print(a.split('c', 3))                     # ['ab', 'deab', 'deab', 'deabcdeabcdeabcde']


[Python] 纯文本查看 复制代码
'''
splitlines(...)
    按照行分隔,返回一个包含各行作为元素的列表,如果 num 指定则仅切片 num 个行
    S.splitlines([keepends]) -> list of strings

    Return a list of the lines in S, breaking at line boundaries.
    Line breaks are not included in the resulting list unless keepends
    is given and true.
'''
print(help(str.splitlines))
a = '11\n22\n33\n44\n55'
print(a.splitlines())                      # ['11', '22', '33', '44', '55']
print(a.splitlines(1))                     # ['11\n', '22\n', '33\n', '44\n', '55']
print(a.splitlines(2))                     # ['11\n', '22\n', '33\n', '44\n', '55']


[Python] 纯文本查看 复制代码
'''
startswith(...)
    用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。
    如果参数 beg 和 end指定值,则在指定范围内检查
    S.startswith(prefix[, start[, end]]) -> bool

    Return True if S starts with the specified prefix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    prefix can also be a tuple of strings to try.
'''
print(help(str.startswith))
a = 'asdfzxcvqwer'
print(a.startswith('a'))                         # True
print(a.startswith('s',1,2))                     # True
print(a.startswith('s',2,3))                     # False


[Python] 纯文本查看 复制代码
'''
strip(...)
    用于移除字符串头尾指定的字符(默认为空格)
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
'''
print(help(str.strip))
a = '   asdfzxcvqwer   '
b = '***asdfzxcvqwer***'
print(a.strip())                        # asdfzxcvqwer
print(b.strip('*'))                     # asdfzxcvqwer




[Python] 纯文本查看 复制代码
'''
swapcase(...)
    用于对字符串的大小写字母进行转换(小写转大写,大写转小写)
    S.swapcase() -> str

    Return a copy of S with uppercase characters converted to lowercase
    and vice versa.
'''
print(help(str.swapcase))
a = 'asdfzxcvqwer'
b = 'ASDFQWERZXCV'
c = 'asdfQWER'
print(a.swapcase())                         # ASDFZXCVQWER
print(b.swapcase())                         # asdfqwerzxcv
print(c.swapcase())                         # ASDFqwer



[Python] 纯文本查看 复制代码
'''
title(...)
    返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写
    S.title() -> str

    Return a titlecased version of S, i.e. words start with title case
    characters, all remaining cased characters have lower case.
'''
print(help(str.title))
a = 'asdfzxcvqwer'
b = 'ASDFQWERZXCV'
c = 'asdfQWER'
print(a.title())                         # Asdfzxcvqwer
print(b.title())                         # Asdfqwerzxcv
print(c.title())                         # Asdfqwer



[Python] 纯文本查看 复制代码
'''
translate(...)
    根据参数table给出的表(包含 256 个字符)转换字符串的字符, 并返回翻译后的字符串。
    要过滤掉的字符放到del 参数中
    S.translate(table) -> str

    Return a copy of the string S, where all characters have been mapped
    through the given translation table, which must be a mapping of
    Unicode ordinals to Unicode ordinals, strings, or None.
    Unmapped characters are left untouched. Characters mapped to None
    are deleted.
'''
print(help(str.translate))




[Python] 纯文本查看 复制代码
'''
upper(...)
    将字符串中的小写字母转为大写字母
    S.upper() -> str

    Return a copy of S converted to uppercase.
'''
print(help(str.upper))
a = 'asdfzxcvqwer'
b = 'asdfQWER'
print(a.upper())                         # ASDFZXCVQWER
print(b.upper())                         # ASDFQWER



[Python] 纯文本查看 复制代码
'''
zfill(...)
    返回一个长度为width的数字字符串,最左边填充0。
    如果width小于等于原字符串长度,则返回原字符串。主要用于数字类字符串的格式化
    S.zfill(width) -> str

    Pad a numeric string S with zeros on the left, to fill a field
    of the specified width. The string S is never truncated.
'''
print(help(str.zfill))
a = '123456'
print(a.zfill(10))                         # 0000123456


他没有一个有个性的自我介绍,你们不要理他!
  • TA的每日心情

    2018-12-23 21:04
  • 签到天数: 166 天

    连续签到: 1 天

    [LV.7]常住居民III

    54

    主题

    69

    帖子

    2199

    积分

    内部成员

    Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15

    积分
    2199
     楼主| 发表于 2018-12-16 18:41:23 | 显示全部楼层
    沙发
    https://whatismyreferer.com
    他没有一个有个性的自我介绍,你们不要理他!
      您需要登录后才可以回帖 登录 | 立即注册

      本版积分规则

      Powered by Discuz! X3.4  © 2001-2013 Comsenz Inc.