# str 在 Python 中代表字符串类型。字符串是用于表示文本数据的序列类型。
# 以下是一些使用 str 的示例代码:
# 创建一个字符串
my_string = "Hello, World!"
print(my_string) # 输出: Hello, World!
# 字符串可以使用单引号或双引号
single_quote_string = 'Python'
double_quote_string = "Python"
print(single_quote_string == double_quote_string) # 输出: True
# 字符串可以进行拼接
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name) # 输出: John Doe
# 字符串可以进行索引和切片
greeting = "Hello, World!"
print(greeting[0]) # 输出: H
print(greeting[7:12]) # 输出: World
# 字符串方法示例
text = " python programming "
print(text.strip()) # 输出: python programming
print(text.upper()) # 输出: PYTHON PROGRAMMING
print(text.replace("python", "Java")) # 输出: Java programming
上一篇:python print 格式化
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站