python中字符串支持哪些运算符呢?
在python中,字符串可以使用以下运算符:
+
*
in
not in
is
is not
==
!=
使用方法举例:
‘+’ 运算符,拼接字符串的作用
s1 = 'hello'
s2 = 'world'
print(s1+s2)
运行结果:
‘*’ 运算符,字符串的倍数
s1 = 'a' * 5
print(s1)
运行结果:
in 运算符,判断是否在字符串中,返回布尔类型 True或False
s1 = 'hello world!'
result = 'w' in s1
print(result)
运行结果:
not in 运算符,判断是否不在字符串,返回布尔类型 False或True
s1 = 'hello world!'
result = 'w' not in s1
print(result)
运行结果:
is 运算符,判断字符串地址是否相同,返回布尔类型 True或False
s1 = 'hello world!'
s2 = 'hello world!'
result = s1 is s2
print(result)
运行结果:
is not 运算符,判断字符串地址是否相同,返回布尔类型 False或True
s1 = 'hello world!'
s2 = 'hello world!'
result = s1 is not s2
print(result)
运行结果:
== 运算符,判断字符串是否相等,返回布尔类型 True或False
s1 = 'hello world!'
s2 = 'hello world!'
result = s1 == s2
print(result)
运行结果:
!= 运算符,判断字符串是否相等,返回布尔类型 False或True
s1 = 'hello world!'
s2 = 'hello world!'
result = s1 != s2
print(result)
运行结果:
如果对你有帮助的话,欢迎评论和点赞!