日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python中的字符轉運算符、字符串處理方式_python

作者:s651665496 ? 更新時間: 2022-09-07 編程語言

字符轉運算符、字符串處理

def CalSingleVals(val1, val2):
    op = ['+', '-', '*', '/']
    rtValList = []
 
    for op1 in op:
        st = str(val1) + op1 + str(val2)
        result = int(eval(st))

默認用法:去除空格

  • str.strip():去除字符串兩邊的空格
  • str.lstrip():去除字符串左邊的空格
  • str.rstrip():去除字符串右邊的空格
def trim(s):
... ? ? import re
... ? ? if s.startswith(' ') or s.endswith(' '):
... ? ? ? ? return re.sub(r"^(\s+)|(\s+)$", "", s)
... ? ? return s

字符串支持的運算符及使用

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)

運行結果:

原文鏈接:https://blog.csdn.net/s651665496/article/details/98100757

欄目分類
最近更新