Python字符串提供了许多实用的方法,可以对字符串进行处理和操作。以下是一些常用的字符串方法:
s = 'hello, world!'
print(s.capitalize()) # 输出Hello, world!
s = 'hello, world!'
print(s.upper()) # 输出HELLO, WORLD!
s = 'HELLO, WORLD!'
print(s.lower()) # 输出hello, world!
s = 'hello, world!'
print(s.title()) # 输出Hello, World!
s = ' hello, world! '
print(s.strip()) # 输出hello, world!
s = 'hello, world!'
print(s.replace('hello', 'hi')) # 输出hi, world!
s = 'hello, world!'
print(s.split(',')) # 输出['hello', ' world!']
s = ['hello', 'world', '!']
print(' '.join(s)) # 输出hello world !