字符串
转义字符
- 用一个特殊的方法表示出一系列不方便写出的内容,如:回车键,换行键,退格键
- 借助反斜杠字符’’,一旦字符串出现反斜杠,反斜杠后面一个或几个字符表示的已经不是原来的意思了,进行了转义
- 在字符串中,一旦出现反斜杠就要加倍小心,可能有转义字符出现
- 不用系统对换行操作有不同的表示
s = 'i love hanxuexue'
print(s)
i love hanxuexue
s = "let's go"
print(s)
ss = 'let\'s go'
print(ss)
sss = "c:\\user"
print(sss)
s1 = 'I love \r\nHanxuexue'
print(s1)
let's go
let's go
c:\user
I love
Hanxuexue
字符串的格式化
- 把字符串按照一定格式进行打印或者填充
- 格式化有两种方法
s = "xxx 您好,欢迎浏览我的优快云博客"
利用百分号格式化
- 在字符串中,利用%表示一个特殊的含义,表示对字符进行格式化
- %d:此处应该放入一个整数
- %s:表示此处应该放入一个字符串
s = "I love %s"
print(s)
I love %s
print("I love %s"%"hanxuexue")
I love hanxuexue
print( s%"hanxuexue")
I love hanxuexue
s = "I am %d years old"
print(s)
print(s%18)
I am %d years old
I am 18 years old
s = "I am %s,I am %d years old"
print(s)
print(s%('xiaoxiao',18))
I am %s,I am %d years old
I am xiaoxiao,I am 18 years old
format函数格式化字符串
- 直接用format函数进行格式化
- 推荐使用这种方法
- 在使用上,以{}和:代替%号,后面用format带参数完成
s = "I love {}".format("hanxuexue")
print(s)
s = "Yes, i am {1} years old,I love {0} and I am {1} years old".format("hanxuexue",18)
print(s)
I love hanxuexue
Yes, i am 18 years old,I love hanxuexue and I am 18 years old
None
- 表示什么都没有
- 如果函数没有返回值,可以返回None
- 用来占位置
- 用来解除变量绑定(以后讲)