網站首頁 編程語言 正文
字符轉運算符、字符串處理
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
相關推薦
- 2022-08-03 C++類與對象深入之引用與內聯函數與auto關鍵字及for循環詳解_C 語言
- 2022-04-20 Python?設計模式創建型單例模式_python
- 2022-12-14 C++?容器中map和unordered?map區別詳解_C 語言
- 2022-10-02 關于react?useState更新異步問題_React
- 2022-11-13 Python中np.random.randint()參數詳解及用法實例_python
- 2022-07-01 .Net設計模式之單例模式(Singleton)_基礎應用
- 2022-07-24 Git版本控制服務器詳解_其它綜合
- 2023-10-30 解決SpringBoot3整合Druid的兼容性問題
- 最近更新
-
- 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同步修改后的遠程分支