温馨提示×

Python如何定义字符串变量

小亿
104
2023-12-29 18:09:08
栏目: 编程语言

在Python中,可以使用单引号或双引号来定义字符串变量。以下是几个示例:

# 使用单引号定义字符串变量
string1 = 'Hello, World!'

# 使用双引号定义字符串变量
string2 = "Python is awesome."

# 使用三个单引号定义多行字符串变量
string3 = '''
This is a multi-line string.
It can contain multiple lines of text.
'''

# 使用三个双引号定义多行字符串变量
string4 = """
This is another multi-line string.
It can also contain multiple lines of text.
"""

请注意,在Python中,字符串是不可变的,这意味着一旦定义了一个字符串,它的值就不能被修改。如果需要对字符串进行修改,可以使用字符串的方法和操作符来创建新的字符串。

0