【Python学习】字符串

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

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())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python中的字符串是一组字符序列,可以使用引号符号(单引号或双引号)来表示字符串字符串Python中是不可变的,这意味着一旦创建了一个字符串,就不能更改其中的字符。 以下是一些常见的字符串操作: 1. 字符串拼接: 可以使用加号(+)来将字符串拼接在一起,也可以使用乘号(*)来重复一个字符串。 例如: ``` str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result) # 输出:Hello World str3 = "A" result = str3 * 5 print(result) # 输出:AAAAA ``` 2. 字符串格式化: 可以使用字符串格式化来将变量的值插入到字符串中。其中,格式化字符串中用花括号({})来表示要插入的值。 例如: ``` name = "Bob" age = 30 result = "My name is {} and I am {} years old".format(name, age) print(result) # 输出:My name is Bob and I am 30 years old ``` 3. 字符串切片: 可以使用下标来获取字符串中的某个字符,也可以使用切片来获取字符串中的一部分。切片操作使用冒号(:)来表示起始位置和结束位置。 例如: ``` str = "Hello World" print(str[0]) # 输出:H print(str[1:5]) # 输出:ello ``` 4. 字符串方法: Python中有很多有用的字符串方法,例如: - `strip()`:去掉字符串中的空格或指定字符 - `lower()`:将字符串中的字母转换为小写 - `upper()`:将字符串中的字母转换为大写 - `replace()`:替换字符串中的指定字符 - `split()`:将字符串按照指定字符分割成列表 例如: ``` str = " Hello World! " print(str.strip()) # 输出:Hello World! print(str.lower()) # 输出:hello world! print(str.upper()) # 输出:HELLO WORLD! print(str.replace("Hello", "Hi")) # 输出: Hi World! print(str.split(" ")) # 输出:['', '', 'Hello', 'World!', '', ''] ``` 总的来说,Python中的字符串操作非常方便和灵活,可以满足大部分字符串处理的需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值