python学习之字符串

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
'''
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值