字符串 与格式化与基本运算

str = "hello world"
print(str[0])   # 打印第几个字符
print(str[0:5])  # 打印第一个到第几个字符
print(str[5:8])   # 左闭右开
print(str[:5])
print(str[-2])   # 打印从右边开始第几个字符

"""
\t 横向制表符 默认4个空格 linux 默认8个空格
\n 换行 
\r 回车 
\"   双引号                 #在双引号字符串里写双引号
\'  单引号               #在单引号字符串里写单引号
\\  失效单个字符特殊含义   #多用于路径文件操作
r  字符串不识别转义符       #多用于路径文件操作

"""

print("h\nel\tlo")
print("hel\rlo world")
print("hello w\"or\"ld")  #在双引号字符串里写双引号
print('hello w\'or\'ld')  #在单引号字符串里写单引号
path = "d:\\tello\image\\temp\huanzhu1.jpg"
print(path)
#  或者  r
path = r"d:\\tello\image\\temp\huanzhu1.jpg"
print(path)               #多用于路径文件操作

控制台结果

h
el  lo
lo world
hello w"or"ld
hello w'or'ld
d:\tello\image\temp\huanzhu1.jpg
d:\\tello\image\\temp\huanzhu1.jpg

字符串 运算

   +      字符串连接
   *       重复输出字符串
   []     索引字符串 中的字符  
   [:]    索引字符串 中的部分字符   
   in  成员运算 
   not  in  成员运算符
a = "hello"
b = "world"
print(a+b)
print(a*2)
print(a[1:5])
print(a[1])

控制台结果

helloworld
hellohello
ello
e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值