学习 字符串 类型,一些常见用法
s = "Hello world 你好,这个世界 1 123 23 234 3456"
center
# center(width, fillchar=' ', /) method of builtins.str instance
# Return a centered string of length width.
print(s.center(50, "-")) # output: -----------Hello world 你好,这个世界 1 23 456-----------
count
# count(...) method of builtins.str instance
# S.count(sub[, start[, end]]) -> int
print(s.count("l", 5, 10)) # 统计指定字符在字符串中出现的次数,output: 1
encode
# encode(encoding='utf-8', errors='strict') method of builtins.str instance
# Encode the string using the codec registered for encoding.
endswith
# endswith(...) method of builtins.str instance
# S.endswith(suffix[, start[, end]]) -> bool
# print(s[18])
print(s.endswith("界", 10, 19)) # output: True
find
# find(...) method of builtins.str instance
# S.find(sub[, start[, end]]) -> int
print(s.find("1", 10, 22)) # 查找指定字符的索引
isdigit
# isdigit() method of builtins.str instance
# Return True if the string is a digit string, False otherwise.
# A string is a digit string if all characters in the string are digits and there
# is at least one character in the string.
a = "5432"
print(a.isdigit())
join
# join(iterable, /) method of builtins.str instance
# Concatenate any number of strings.
# The string whose method is called is inserted in between each given string.
# The result is returned as a new string.
# Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
print("-".join(s)) # output: H-e-l-l-o- -w-o-r-l-d- -你-好-,-这-个-世-界- -1- -1-2-3- -2-3- -2-3-4- -3-4-5-6
replace
# replace(old, new, count=-1, /) method of builtins.str instance
# Return a copy with all occurrences of substring old replaced by new.
# count
# Maximum number of occurrences to replace.
# -1 (the default value) means replace all occurrences.
# If the optional argument count is given, only the first count occurrences are replaced.
print(s.replace("你好", "大家好")) # output: Hello world 大家好,这个世界 1 123 23 234 3456
print(s.replace("l", "W", 2)) # output: HeWWo world 你好,这个世界 1 123 23 234 3456
split
# split(sep=None, maxsplit=-1) method of builtins.str instance
# Return a list of the substrings in the string, using sep as the separator string.
# sep
# The separator used to split the string.
# When set to None (the default value), will split on any whitespace
# character (including \\n \\r \\t \\f and spaces) and will discard
# empty strings from the result.
# maxsplit
# Maximum number of splits (starting from the left).
# -1 (the default value) means no limit.
# Note, str.split() is mainly useful for data that has been intentionally delimited. With natural text that includes punctuation, consider using the regular expression module.
print(s.split()) # output: ['Hello', 'world', '你好,这个世界', '1', '123', '23', '234', '3456']
print(s.split(",")) # output: ['Hello world 你好', '这个世界 1 123 23 234 3456']
strip
# strip(chars=None, /) method of builtins.str instance
# Return a copy of the string with leading and trailing whitespace removed.
# If chars is given and not None, remove characters in chars instead.
b = " <this is BEIJING railway stat "
print(b.strip("is"))
print(b.strip())