首页 > 编程语言 > 详细

Python字符串运算符

时间:2020-02-08 13:47:30      阅读:65      评论:0      收藏:0      [点我收藏+]

Python字符串运算符:

  + :连接左右两端的字符串。

  *  :重复输出字符串。

  [ ] :通过索引获取字符串中的值。

  [start:stop:step]:开始,结束位置的后一个位置,步长。

  in  :判断左端的字符是否在右面的序列中。

  not in:判断左端的字符是否不在右面的序列中。

  r/R :在字符串开头使用,使转义字符失效。

# 字符串使用 + 号
strs = "hello " + "world."
print(strs)
# hello world.

# 字符串使用 * 号
strs = abc 
# 无论数字在哪一端都可以
print(3*strs)
# abc abc abc

print(strs * 3)
# abc abc abc

# 使用索引下标
strs = "hello world."
print(strs[4])
# o
print(strs[7])
# o

# 切片操作,左闭右开原则
strs = "hello world."
# 将字符串倒序输出
print(strs[::-1])
# .dlrow olleh

print(strs[6:11:])
# world

strs = "ABCDEFG"
print("D" in strs)
# True
print("L" in strs)
# False

print("D" not in strs)
# False
print("L" not in strs)
# True

# 使用 r 使字符串中的转义字符失效
print(a\tb)
# a    b
print(ra\tb)
# a\tb

2020-02-08

Python字符串运算符

原文:https://www.cnblogs.com/hany-postq473111315/p/12275891.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!