# str在Python中的含义
# str是Python中表示字符串的数据类型。字符串是由字符组成的序列,可以包含字母、数字、符号等。
# 示例1:创建字符串
example_str = "Hello, World!"
print(example_str) # 输出: Hello, World!
# 示例2:使用单引号或双引号创建字符串
single_quote_str = 'This is a string.'
double_quote_str = "This is also a string."
print(single_quote_str) # 输出: This is a string.
print(double_quote_str) # 输出: This is also a string.
# 示例3:多行字符串
multi_line_str = """这是一个多行字符串,
它可以跨越多行,
并且保留格式。"""
print(multi_line_str)
# 示例4:字符串的常用操作
# 字符串拼接
concatenated_str = "Hello" + " " + "World"
print(concatenated_str) # 输出: Hello World
# 字符串长度
length_of_str = len("Hello")
print(length_of_str) # 输出: 5
# 字符串索引和切片
s = "Python"
print(s[0]) # 输出: P (第一个字符)
print(s[-1]) # 输出: n (最后一个字符)
print(s[1:4]) # 输出: yth (从索引1到索引3的子字符串)
# 示例5:字符串方法
# 转换为大写
upper_str = "hello".upper()
print(upper_str) # 输出: HELLO
# 替换字符串中的内容
replaced_str = "Hello, World!".replace("World", "Python")
print(replaced_str) # 输出: Hello, Python!
上一篇:python多线程
下一篇:python class
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站