Python基础(一)

xiaoxiao2021-02-28  105

1、输出指定的单行字符串

print('This is a string!')

--这中间的所有空白,即空格、制表符都正常保留输出

使用‘’和""效果一样

例如:

print("What's you name?")

需要注意的是当输出字符串中出现’时,例如输出What's you name?此时就不能使用''单引号表示,因为Python不知道字符串的起始和技术位置

print("This is a string too")

2、输出多行字符串

方式一:利用三引号,"""或者'''

例如:

print('''This a multi-line string. This is the first line. "What's you name?" ''')

print("""This a multi-line string too . This is the first line too . "What's you name?.I asked" """)

 方式二:利用转义字符

例如:

利用单引号输出What's you name?

print('What\'s you name?')

类似的在使用双引号的字符串中出现双引号必须使用转义浮

另:必须使用转义浮\\来表示反斜杠

>>> print('a\\b\\c\\')

a\b\c\

需要说明的是,在一个字符串中,在一行末尾的反斜杠仅仅表示下一行的字符串是上一行的继续,并不能增加新一行。

例如:

print("This is the first sentence.\ This is the second sentence.")

等同于

print("This is the first sentence.This is the second sentence.")

输出两行或多行字符串,可以使用转义符\n表示新的一行的开始

例如:

print('This is the first line.\nThis is the second line.')

另一个有用的转义符 \t

例如:

print('This is the firset line.\tThis is the second line')

效果等同于Tab键

常见转义符含义

符号含义说明\'单引号\"双引号\a发出系统提示音\b退格符\n换行符\t横向制表符\v纵向制表符\r回车符\f换页符\o八进制数代表的字符\x十六进制数代表的字符\0表示一个空字符\\反斜杠

字符串一旦创建,是不能被改变的

如果你需要一个新的字符串,那么你需要创建一个新的——字符串的不可变性

转载请注明原文地址: https://www.6miu.com/read-33298.html

最新回复(0)