# Python字符串操作示例
# 定义一个字符串
my_string = "Hello, World!"
# 打印字符串
print(my_string) # 输出: Hello, World!
# 字符串长度
length = len(my_string)
print(f"字符串的长度是: {length}") # 输出: 字符串的长度是: 13
# 字符串切片
substring = my_string[0:5]
print(f"截取的子字符串是: {substring}") # 输出: 截取的子字符串是: Hello
# 字符串拼接
new_string = my_string + " Welcome to Python!"
print(new_string) # 输出: Hello, World! Welcome to Python!
# 字符串转大写
upper_string = my_string.upper()
print(f"大写的字符串是: {upper_string}") # 输出: 大写的字符串是: HELLO, WORLD!
# 字符串转小写
lower_string = my_string.lower()
print(f"小写的字符串是: {lower_string}") # 输出: 小写的字符串是: hello, world!
# 替换字符串中的内容
replaced_string = my_string.replace("World", "Python")
print(f"替换后的字符串是: {replaced_string}") # 输出: 替换后的字符串是: Hello, Python!
# 检查字符串是否以指定字符开头
starts_with_hello = my_string.startswith("Hello")
print(f"字符串是否以'Hello'开头: {starts_with_hello}") # 输出: 字符串是否以'Hello'开头: True
# 检查字符串是否以指定字符结尾
ends_with_exclamation = my_string.endswith("!")
print(f"字符串是否以'!'结尾: {ends_with_exclamation}") # 输出: 字符串是否以'!'结尾: True
上一篇:python如何运行
下一篇:python取余数的运算符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站