Python字符串运算符

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	b')
# a    b
print(r'a	b')
# a	b

2020-02-08

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