# Python字符串操作示例代码
# 1. 字符串拼接
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # 输出: Hello World
# 2. 字符串长度
length = len("Hello")
print(length) # 输出: 5
# 3. 字符串切片
s = "HelloWorld"
print(s[0:5]) # 输出: Hello
print(s[5:]) # 输出: World
# 4. 字符串查找
s = "HelloWorld"
index = s.find("World")
print(index) # 输出: 5
# 5. 字符串替换
s = "HelloWorld"
new_s = s.replace("World", "Python")
print(new_s) # 输出: HelloPython
# 6. 字符串分割
s = "apple,banana,orange"
fruits = s.split(",")
print(fruits) # 输出: ['apple', 'banana', 'orange']
# 7. 字符串去空格
s = " Hello World "
trimmed_s = s.strip()
print(trimmed_s) # 输出: Hello World
# 8. 字符串大小写转换
s = "HelloWorld"
lowercase_s = s.lower()
uppercase_s = s.upper()
print(lowercase_s) # 输出: helloworld
print(uppercase_s) # 输出: HELLOWORLD
# 9. 字符串格式化
name = "Alice"
age = 30
formatted_str = f"My name is {name} and I am {age} years old."
print(formatted_str) # 输出: My name is Alice and I am 30 years old.
下一篇:python url编码
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站