python字符串
字符串的表示
在python中’'和“”都可以表示字符串
a="str"
b='str'
print(a,b)
'''
输出:str str
'''
但当输出的字符串中包含’(“)时就不能使用’(”)来表示字符串,会产生错误,选择交叉避开相同符号表示的问题
b='Let's go '
'''输出
Input In [10]
b='Let's go '
^
SyntaxError: invalid syntax
'''
#选择交叉避开相同符号表示的问题
b="Let's go "#输出内容包含单引号,就不适合用单引号表示字符
print(b)
c='"Happy today! Happy everyday!"'#输出内容包含单引号,就不适合用单引号表示字符
print(c)
'''输出
Let's go
"Happy today! Happy everyday!"
'''
#上述问题也可以使用转义字符解决
b=" \"Let's go.\" "
print(b)
常见的转义字符以及作用
转义字符 | 作用 |
---|---|
’ | 单引号 |
" | 双引号 |
\n | 换行 |
| | 反斜杠符号 |
\t | 横向制表符 |
\b | 退格 |
\r | 回车 |
长字符串表示
1.用\n
2.用”””…””” 或’’’…’’’(注意父号个数和类型要匹配)
#法一
str='''(1) Variable names cannot start with a number
(2) Variable name are case sensitive
(3) Variable names can be used by men
(4) x, y=y, x can achieve x, y value swap string (string)'''
print(str)
#法二
strr='(1) Variable names cannot start with a number\n(2) Variable name are case sensitive\n(3) Variable names can be used by men\n(4) x, y=y, x can achieve x, y value swap string (string)'
print(strr)
'''输出
(1) Variable names cannot start with a number
(2) Variable name are case sensitive
(3) Variable names can be used by men
(4) x, y=y, x can achieve x, y value swap string (string)
'''
控制台读取字符串
从控制台读取的是字符串,需要将其转化为你想要的类型,以下代码时将其转化为int类型
temp=input('不妨猜一下小甲鱼现在心里想的那个数字:')
guess=int(temp)#转化为int类型
数据类型转化
http://t.csdn.cn/OUg3W详细介绍了类型的转化
字符串的加法和乘法
加法:即为拼接
'23'+'23'
#输出 '2323'
乘法:重复
print("我newbee\n"*520)
'''输出
我newbee
我newbee
我newbee
......
我newbee
我newbee
我newbee
我newbee
'''
文件路径表示
1.双杠\
2.单杠/
print('D:\\one\\two\\three')
print('D:/one/two/three')
'''
输出
D:\one\two\three
D:/one/two/three
'''