最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年07月15日 10:16:33   作者:s651665496  
這篇文章主要介紹了python中的字符轉運算符、字符串處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

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

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)

運行結果:

‘*’ 運算符,字符串的倍數(shù)

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)

運行結果:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

呼图壁县| 伊吾县| 桦甸市| 抚宁县| 顺义区| 邮箱| 绥滨县| 北京市| 吴川市| 奎屯市| 青州市| 曲松县| 安泽县| 云龙县| 景宁| 元氏县| 汪清县| 海晏县| 且末县| 如东县| 宁海县| 睢宁县| 晋江市| 砀山县| 乌海市| 长葛市| 嵊州市| 彰化县| 江津市| 惠东县| 鞍山市| 建昌县| 安福县| 高台县| 林周县| 北宁市| 利津县| 枣强县| 石门县| 和田市| 同仁县|