Python中str是一种字符串类型的数据结构,用于表示文本信息。可以通过以下方式使用str:
s = "Hello, World!"
print(s[0]) # 输出:H
print(len(s)) # 输出:13
print(s[7:12]) # 输出:World
s1 = "Hello"
s2 = "World"
s3 = s1 + ", " + s2
print(s3) # 输出:Hello, World
name = "Alice"
age = 25
print("My name is %s and I'm %d years old." % (name, age)) # 输出:My name is Alice and I'm 25 years old.
s = "Hello, World!"
print(s.find("World")) # 输出:7
print(s.replace("Hello", "Hi")) # 输出:Hi, World!
s = "Hello"
print(s.lower()) # 输出:hello
print(s.upper()) # 输出:HELLO
s = "Hello, World!"
print(s.split(",")) # 输出:['Hello', ' World!']
print(" ".join(['Hello', 'World!'])) # 输出:Hello World!
s = "Hello, World!"
print(s.startswith("Hello")) # 输出:True
print(s.endswith("World!")) # 输出:True
这些是常见的字符串操作方法,可以根据需要来使用。