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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Pandas常用的讀取和保存數(shù)據(jù)的函數(shù)使用(csv,mysql,json,excel)
本文主要介紹了Pandas常用的讀取和保存數(shù)據(jù)的函數(shù)使用,主要包括csv,mysql,json,excel這幾種方式,具有一定的參考價值,感興趣的可以了解一下2022-01-01對python中的try、except、finally 執(zhí)行順序詳解
今天小編就為大家分享一篇對python中的try、except、finally 執(zhí)行順序詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02全面解析Python的While循環(huán)語句的使用方法
這篇文章主要介紹了全面解析Python的While循環(huán)語句的使用方法,是Python入門學習中的基礎知識,需要的朋友可以參考下2015-10-10python爬取w3shcool的JQuery課程并且保存到本地
本文主要介紹python爬取w3shcool的JQuery的課程并且保存到本地的方法解析。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Python 數(shù)據(jù)結構之隊列的實現(xiàn)
這篇文章主要介紹了Python 數(shù)據(jù)結構之隊列的實現(xiàn)的相關資料,需要的朋友可以參考下2017-01-01