How does Python define string variables?

In Python, you can use either single quotes or double quotes to define string variables. Here are some examples:

# 使用单引号定义字符串变量
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.
"""

Please note that in Python, strings are immutable, which means once a string is defined, its value cannot be changed. If you need to modify a string, you can use string methods and operators to create a new string.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds