python中的字符轉(zhuǎn)運(yùn)算符、字符串處理方式
字符轉(zhuǎn)運(yùn)算符、字符串處理
def CalSingleVals(val1, val2): op = ['+', '-', '*', '/'] rtValList = [] for op1 in op: st = str(val1) + op1 + str(val2) result = int(eval(st))
默認(rèn)用法:去除空格
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
字符串支持的運(yùn)算符及使用
python中字符串支持哪些運(yùn)算符呢?
在python中,字符串可以使用以下運(yùn)算符:
+
*
in
not in
is
is not
==
!=
使用方法舉例
‘+’ 運(yùn)算符,拼接字符串的作用
s1 = 'hello' s2 = 'world' print(s1+s2)
運(yùn)行結(jié)果:
‘*’ 運(yùn)算符,字符串的倍數(shù)
s1 = 'a' * 5 print(s1)
運(yùn)行結(jié)果:
in 運(yùn)算符,判斷是否在字符串中,返回布爾類型 True或False
s1 = 'hello world!' result = 'w' in s1 print(result)
運(yùn)行結(jié)果:
not in 運(yùn)算符,判斷是否不在字符串,返回布爾類型 False或True
s1 = 'hello world!' result = 'w' not in s1 print(result)
運(yùn)行結(jié)果:
is 運(yùn)算符,判斷字符串地址是否相同,返回布爾類型 True或False
s1 = 'hello world!' s2 = 'hello world!' result = s1 is s2 print(result)
運(yùn)行結(jié)果:
is not 運(yùn)算符,判斷字符串地址是否相同,返回布爾類型 False或True
s1 = 'hello world!' s2 = 'hello world!' result = s1 is not s2 print(result)
運(yùn)行結(jié)果:
== 運(yùn)算符,判斷字符串是否相等,返回布爾類型 True或False
s1 = 'hello world!' s2 = 'hello world!' result = s1 == s2 print(result)
運(yùn)行結(jié)果:
!= 運(yùn)算符,判斷字符串是否相等,返回布爾類型 False或True
s1 = 'hello world!' s2 = 'hello world!' result = s1 != s2 print(result)
運(yùn)行結(jié)果:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Pandas常用的讀取和保存數(shù)據(jù)的函數(shù)使用(csv,mysql,json,excel)
本文主要介紹了Pandas常用的讀取和保存數(shù)據(jù)的函數(shù)使用,主要包括csv,mysql,json,excel這幾種方式,具有一定的參考價(jià)值,感興趣的可以了解一下2022-01-01對python中的try、except、finally 執(zhí)行順序詳解
今天小編就為大家分享一篇對python中的try、except、finally 執(zhí)行順序詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02全面解析Python的While循環(huán)語句的使用方法
這篇文章主要介紹了全面解析Python的While循環(huán)語句的使用方法,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10python爬取w3shcool的JQuery課程并且保存到本地
本文主要介紹python爬取w3shcool的JQuery的課程并且保存到本地的方法解析。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04Python 數(shù)據(jù)結(jié)構(gòu)之隊(duì)列的實(shí)現(xiàn)
這篇文章主要介紹了Python 數(shù)據(jù)結(jié)構(gòu)之隊(duì)列的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-01-01Python 動(dòng)態(tài)變量名定義與調(diào)用方法
這篇文章主要介紹了Python 動(dòng)態(tài)變量名定義與調(diào)用方法,需要的朋友可以參考下2020-02-02Python使用Rich實(shí)現(xiàn)美化終端顯示效果
Rich庫的功能就像它的名字一樣,使Python編程更加豐富(rich),用來幫助開發(fā)者在控制臺(tái)(命令行)輸出中創(chuàng)建豐富、多彩和具有格式化的文本,下面我們就來了解下它的具體使用吧2024-02-02