網站首頁 編程語言 正文
python數值與字符串高級用法
1.概述
這篇是一篇沒有盡頭的文章,每當過段時間,再次打開就會看到不一樣的內容,有新東西在更新啊。是啊,之所以取名為高級用法,就是因為它是連載的,一個個有趣的知識點就像是一個個故事。每讀一遍都有新的收獲。
2.數值
2.1.美化數值
在定義數值字面量時,如果數字特別長可以通過插入_分隔符來讓他變得更易讀
# 格式化數值:在定義數值字面量時,如果數字特別長,可以通過插入_分隔符來變得更易讀
# 以千為分隔單位,輸出結果(num值為:10000)
num = 1_000_0
print(f'num值為:{num}')
# 輸出結果
num值為:10000
補充:Python數值和字符串
數值類型
## 整型
i0 = 3
#int(x)這種方法通常在類型轉換的時候使用,python定義數字一般就用最簡單的
i1 = int(2)
print("i0 = {}".format(i0))
print("i1 = {}".format(i1))
## 浮點型
f0 = 3.14
f1=float(3.1415)
print("f0 = {}".format(f0))
print("f1 = {}".format(f1))
## bool型
#bool型是int型子類
print("isinstance(True,int):{}".format(isinstance(True,int)))
print("issubclass(bool,int):{}".format(issubclass(bool,int)))
## 算數運算
#+ - * / % // **
#/ 始終返回一個浮點數
#// 地板除,返回值取決于除數和被除數類型
#% 公式:a%b = a - (a/b)*b python中結果向負無窮方向舍入,以b的結果為準 golang中結果正負以a為準
print("i0/i1 = {}".format(i0/i1))
print("i0//i1 = {}".format(i0//i1))
print("f0//f1 = {}".format(f0//f1))
print("i0**i1 = {}".format(i0**i1))
## 邏輯運算
#< > <= >= == !=
print("f1 > i1 : {}".format(f1>i1))
# is 用于比較是否是同一個對象
## 位運算
# << 左移一位相當于*2
# >> 右移一位相當于/2
print("i1 << 1:{}".format(i1<<1))
## 進制轉換
#bin() 轉換為二進制
#oct() 轉換為八進制
#hex() 轉換為十六進制
print("dec:{}->bin:{}".format(i1,bin(i1)))
## 類型轉換
print("float:{}->int:{}".format(f1,int(f1)))
## math庫
#import math
------------------------------
i0 = 3
i1 = 2
f0 = 3.14
f1 = 3.1415
isinstance(True,int):True
issubclass(bool,int):True
i0/i1 = 1.5
i0//i1 = 1
f0//f1 = 0.0
i0**i1 = 9
f1 > i1 : True
i1 << 1:4
dec:2->bin:0b10
float:3.1415->int:3
字符/字符串
####定義
# 用 "" 或者 ''
s1 = 'abcde'
s2 = "abcd"
print("s1 type is {}".format(type(s1)))
print("s2 type is {}".format(type(s2)))
print("s1:{}".format(s1))
####方法
### 增
# s1 = str.join(s2) 括號中為可迭代對象,在s2中每兩個字符之間插入s1,返回新字符串
lst1 = ["x", "y", "z"]
s3 = s1.join(s2)
print("s3:{}".format(s3))
s4 = s1.join(lst1)
print("s4:{}".format(s4))
### 刪
### 改
##str.split(seq=None,maxsplit=-1)
# 將字符串按seq分割為若干片段,默認seq為空白字符(空格,tab等),返回列表
# maxsplit 指定分割次數,-1表示遍歷字符串,分割1次結果為兩段
# str.rsplit() 作用是從右側開始分割
# str.splitlines([keepends]) 按換行符切換 [True]表示保留換行符
s5 = "0a1a2a3a4a5a6"
print("s5.split:{}".format(s5.split("a")))
##str.upper() 全大寫
print("s1.upper():{}".format(s1.upper()))
##str.lower() 全小寫
print("s1.lower():{}".format(s1.lower()))
##str.swapcase() 大小寫互換
print("s1.swapcase():{}".format(s1.swapcase()))
##str.replace(old,new[,count]) 將字符串中的old替換為new,count為替換次數,返回新字符串
print("s5.replace(\"a\",\"b\",3):{}".format(s5.replace("a", "b", 3)))
##str.strip([str]) 去除兩側的字符串, str.lstrip() 左側去除 str.rstrip() 右側去除
s6 = "abc xxx def"
print("s6.strip(\"abc\"):{}".format(s6.strip("abc")))
##拼接 直接用加號拼接
### 查
##索引,通過索引索引 str[i]
##切片 str[begin:end:step] 從begin到end 步長為step,當step為負數的時候表示從右到左,str[::-1]逆序輸出
print("s6[0:4:2]:{}".format(s6[0:4:2]))
print("s6[::-]:{}".format(s6[::-1]))
##len(str)
print("len(s1):{}".format(len(s1)))
##str.count() 查找字符串中子串出現的次數
print("s6.count(\"abc\"):{}".format(s6.count("abc")))
##str.find(sub[,begin[,end]]) 從左到右,左開右閉,返回索引,找不到返回-1
print("s1.find(\"a\"):{}".format(s1.find("a")))
##str.rfind(sub[,begin[,end]]) 從右到左,左開右閉,返回索引,找不到返回-1
print("s1.rfind(\"z\"):{}".format(s1.find("z")))
##str.index(sub[,begin[,end]]) 從左到右,左開右閉,返回索引,找不到返回 ValueError: substring not found
print("s1.index(\"a\"):{}".format(s1.index("a")))
##str.rindex(sub[,begin[,end]]) 從右到左,左開右閉,返回索引,找不到返回 ValueError: substring not found
# print("s1.rindex(\"z\"):{}".format(s1.rindex("z"))) ValueError: substring not found
##str.endswith(suffix[,start[,end]]) 判斷str是否以suffix結尾,返回bool值
print("s6.endswith(\"abc\"):{}".format(s6.endswith("abc")))
##str.startswith(prefix[,start[,end]]) 判斷str是否以prefix結尾,返回bool值
print("s6.startswith(\"xyz\"):{}".format(s6.startswith("xyz")))
##str.isdigit() 判斷字符串是不是只有數字,返回bool型
print("s1.isdigit():{}".format(s1.isdigit()))
##str.isalpha() 判斷字符串是不是只有字母,返回bool型
print("s1.isalpha():{}".format(s1.isalpha()))
##str.isupper() 判斷字符串是不是只有大寫字母,返回bool型
print("s1.isupper():{}".format(s1.isupper()))
####補充
# str是不可變對象
# r/R后接字符串表示字符串中所有字符都不做轉義
print("'12345\\n' " + '12345\n')
print("r'12345\\n' " + r'12345\n')
----------------------------------------------
s1 type is <class 'str'>
s2 type is <class 'str'>
s1:abcde
s3:aabcdebabcdecabcded
s4:xabcdeyabcdez
s5.split:['0', '1', '2', '3', '4', '5', '6']
s1.upper():ABCDE
s1.lower():abcde
s1.swapcase():ABCDE
s5.replace("a","b",3):0b1b2b3a4a5a6
s6.strip("abc"): xxx def
s6[0:4:2]:ac
s6[::-]:fed xxx cba
len(s1):5
s6.count("abc"):1
s1.find("a"):0
s1.rfind("z"):-1
s1.index("a"):0
s6.endswith("abc"):False
s6.startswith("xyz"):False
s1.isdigit():False
s1.isalpha():True
s1.isupper():False
'12345\n' 12345
r'12345\n' 12345\n
原文鏈接:https://blog.csdn.net/m0_38039437/article/details/126293220
相關推薦
- 2022-10-27 Kotlin?Flow常用封裝類StateFlow使用詳解_Android
- 2023-02-12 react-router-domV6嵌套路由實現詳解_React
- 2022-06-25 React?Hooks與setInterval的踩坑問題小結_React
- 2022-06-22 C++深入探究類與對象之友元與運算符重載_C 語言
- 2022-06-27 Python使用re模塊實現okenizer(表達式分詞器)_python
- 2022-11-23 .NET?SkiaSharp?生成二維碼驗證碼及指定區域截取方法實現_實用技巧
- 2023-06-05 C++頭文件和cpp文件的原理分析_C 語言
- 2022-06-15 C語言詳解實現字符菱形的方法_C 語言
- 最近更新
-
- 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同步修改后的遠程分支