【Python学习】字符串

本文介绍了Python字符串的一些常用方法,如center、count、encode、endswith、find、isdigit、join、replace、split和strip。这些方法涵盖了字符串的格式化、计数、编码转换、查找、替换、分割和清理等操作,是字符串处理的基础工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习 字符串 类型,一些常见用法

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())
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值