在Python中,字符串可以通过以下几种方式表示:
1. 单引号(`'`):
string1 = 'hello world'
2. 双引号(`"`):
string2 = "hello world"
3. 三引号(`'''` 或 `"""`):
string3 = '''hello world'''
string4 = """hello world"""
三引号通常用于表示多行字符串,并且可以包含换行符。
如果需要在字符串中包含引号字符,可以使用转义字符 `\`:
string_with_quotes = 'He said, "Hello!"'
或者使用原始字符串(在字符串前加 `r` 或 `R`):
raw_string = r'He said, "Hello!"'
在原始字符串中,转义字符 `\` 不会被解释为特殊字符。