網站首頁 編程語言 正文
字符轉運算符、字符串處理
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
相關推薦
- 2023-01-03 go?doudou開發gRPC服務快速上手實現詳解_Golang
- 2022-08-15 前端寫代碼的時候,不滿足條件程序停止執行下面的程序,并彈窗提示
- 2022-03-26 C++中靜態數據成員使用示例_C 語言
- 2023-02-01 C++泛型編程綜合講解_C 語言
- 2022-04-04 【mybatis】spring mybatis與pageHelper分頁插件的整合
- 2022-03-11 C語言中static的使用介紹_C 語言
- 2022-11-01 JetPack?Compose底部導航欄的實現方法詳解_Android
- 2022-11-08 切換tab時,van-list中的onload事件沒觸發
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支