-
用Python处理字符串的35种常见操作,每个操作都附有示例和输出结果:
1.字符串长度
s = "Hello, World!"
length = len(s)
print(length) # 输出: 13
2.转换为大写
s = "hello, world!"
upper_s = s.upper()
print(upper_s) # 输出: HELLO, WORLD!
3.转换为小写
s = "HELLO, WORLD!"
lower_s = s.lower()
print(lower_s) # 输出: hello, world!
4.首字母大写
s = "hello, world!"
capitalized_s = s.capitalize()
print(capitalized_s) # 输出: Hello, world!
5.标题格式
s = "hello, world!"
title_s = s.title()
print(title_s) # 输出: Hello, World!
6.去除两端空格
s = " hello, world! "
stripped_s = s.strip()
print(stripped_s) # 输出: hello, world!
7.去除左端空格
s = " hello, world! "
lstripped_s = s.lstrip()
print(lstripped_s) # 输出: hello, world!
8.去除右端空格
s = " hello, world! "
rstripped_s = s.rstrip()
print(rstripped_s) # 输出: hello, world!
9.替换子字符串
s = "hello, world!"
replaced_s = s.replace("world", "Python")
print(replaced_s) # 输出: hello, Python!
10.查找子字符串
s = "hello, world!"
index = s.find("world")
print(index) # 输出: 7
11.字符串分割
s = "hello, world!"
split_s = s.split(", ")
print(split_s) # 输出: ['hello', 'world!']
12.检查字符串是否以某个子字符串开头
s = "hello, world!"
starts_with = s.startswith("hello")
print(starts_with) # 输出: True
13.检查字符串是否以某个子字符串结尾
s = "hello, world!"
ends_with = s.endswith("world!")
print(ends_with) # 输出: True
14.转换为列表
s = "hello"
char_list = list(s)
print(char_list) # 输出: ['h', 'e', 'l', 'l', 'o']
15.字符串拼接
strings = ["hello", "world"]
joined_s = " ".join(strings)
print(joined_s) # 输出: hello world
16.重复字符串
s = "hello"
repeated_s = s * 3
print(repeated_s) # 输出: hellohellohello
17.检查是否全是字母
s = "hello"
is_alpha = s.isalpha()
print(is_alpha) # 输出: True
18.检查是否全是数字
s = "12345"
is_digit = s.isdigit()
print(is_digit) # 输出: True
19.检查是否全是空白字符
s = " "
is_space = s.isspace()
print(is_space) # 输出: True
20.检查是否全是小写字母
s = "hello"
is_lower = s.islower()
print(is_lower) # 输出: True
21.检查是否全是大写字母
s = "HELLO"
is_upper = s.isupper()
print(is_upper) # 输出: True
22.检查字符串是否为标题格式
s = "Hello World"
is_title = s.istitle()
print(is_title) # 输出: True
23.字符串居中
s = "hello"
centered_s = s.center(10)
print(centered_s) # 输出: ' hello '
24.字符串左对齐
s = "hello"
left_aligned_s = s.ljust(10)
print(left_aligned_s) # 输出: 'hello '
25.字符串右对齐
s = "hello"
right_aligned_s = s.rjust(10)
print(right_aligned_s) # 输出: ' hello'
26.填充字符
s = "42"
padded_s = s.zfill(5)
print(padded_s) # 输出: '00042'
27.格式化字符串
name = "Alice"
age = 30
formatted_s = f"My name is {name} and I am {age} years old."
print(formatted_s) # 输出: My name is Alice and I am 30 years old.
28.转换为字节数组
s = "hello"
byte_array = s.encode()
print(byte_array) # 输出: b'hello'
29.从字节数组转换为字符串
byte_array = b'hello'
s = byte_array.decode()
print(s) # 输出: hello
30.字符串反转
s = "hello"
reversed_s = s[::-1]
print(reversed_s) # 输出: olleh
31.插入字符
s = "HelloWorld"
inserted_s = s[:5] + " " + s[5:]
print(inserted_s) # 输出: Hello World
32.获取子字符串
s = "Hello, World!"
substring = s[7:12]
print(substring) # 输出: World
33.删除特定字符
s = "Hello, World!"
removed_s = s.replace("o", "")
print(removed_s) # 输出: Hell, Wrld!
34.检查是否全是ASCII字符
s = "Hello"
is_ascii = s.isascii()
print(is_ascii) # 输出: True
35.字符串翻转每个单词
s = "Hello, World!"
reversed_words = " ".join(word[::-1] for word in s.split())
print(reversed_words) # 输出: ,olleH !dlroW
-
这些操作涵盖了常见的字符串处理需求,包括大小写转换、查找、替换、拆分、格式化等.希望这些示例能帮助你更好地理解和使用Python进行字符串处理.
-
感谢大家的关注和支持!想了解更多Python编程精彩知识内容,请关注我的 微信公众号:python小胡子,有最新最前沿的的python知识和人工智能AI与大家共享,同时,如果你觉得这篇文章对你有帮助,不妨点个赞,并点击关注.动动你发财的手,万分感谢!!!
-
原创文章不易,求点赞、在看、转发或留言,这样对我创作下一个精美文章会有莫大的动力!