日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

詳解python數(shù)值與字符串高級(jí)用法_python

作者:Bruce小鬼 ? 更新時(shí)間: 2022-10-07 編程語(yǔ)言

python數(shù)值與字符串高級(jí)用法

1.概述

這篇是一篇沒(méi)有盡頭的文章,每當(dāng)過(guò)段時(shí)間,再次打開就會(huì)看到不一樣的內(nèi)容,有新東西在更新啊。是啊,之所以取名為高級(jí)用法,就是因?yàn)樗沁B載的,一個(gè)個(gè)有趣的知識(shí)點(diǎn)就像是一個(gè)個(gè)故事。每讀一遍都有新的收獲。

2.數(shù)值

2.1.美化數(shù)值

在定義數(shù)值字面量時(shí),如果數(shù)字特別長(zhǎng)可以通過(guò)插入_分隔符來(lái)讓他變得更易讀

# 格式化數(shù)值:在定義數(shù)值字面量時(shí),如果數(shù)字特別長(zhǎng),可以通過(guò)插入_分隔符來(lái)變得更易讀
# 以千為分隔單位,輸出結(jié)果(num值為:10000)
num = 1_000_0
print(f'num值為:{num}')
# 輸出結(jié)果
num值為:10000

補(bǔ)充:Python數(shù)值和字符串

數(shù)值類型

## 整型
i0 = 3
#int(x)這種方法通常在類型轉(zhuǎn)換的時(shí)候使用,python定義數(shù)字一般就用最簡(jiǎn)單的
i1 = int(2)
print("i0 = {}".format(i0))
print("i1 = {}".format(i1))

## 浮點(diǎn)型
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)))

## 算數(shù)運(yùn)算
#+ - * / % // **
#/ 始終返回一個(gè)浮點(diǎn)數(shù)
#// 地板除,返回值取決于除數(shù)和被除數(shù)類型
#%  公式:a%b = a - (a/b)*b    python中結(jié)果向負(fù)無(wú)窮方向舍入,以b的結(jié)果為準(zhǔn)  golang中結(jié)果正負(fù)以a為準(zhǔn)
print("i0/i1 = {}".format(i0/i1))
print("i0//i1 = {}".format(i0//i1))
print("f0//f1 = {}".format(f0//f1))
print("i0**i1 = {}".format(i0**i1))

## 邏輯運(yùn)算
#< > <= >= == !=
print("f1 > i1 : {}".format(f1>i1))
# is 用于比較是否是同一個(gè)對(duì)象

## 位運(yùn)算
# << 左移一位相當(dāng)于*2
# >> 右移一位相當(dāng)于/2
print("i1 << 1:{}".format(i1<<1))

## 進(jìn)制轉(zhuǎn)換
#bin() 轉(zhuǎn)換為二進(jìn)制
#oct() 轉(zhuǎn)換為八進(jìn)制
#hex() 轉(zhuǎn)換為十六進(jìn)制
print("dec:{}->bin:{}".format(i1,bin(i1)))

## 類型轉(zhuǎn)換
print("float:{}->int:{}".format(f1,int(f1)))

## math庫(kù)
#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) 括號(hào)中為可迭代對(duì)象,在s2中每?jī)蓚€(gè)字符之間插入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分割為若干片段,默認(rèn)seq為空白字符(空格,tab等),返回列表
# maxsplit 指定分割次數(shù),-1表示遍歷字符串,分割1次結(jié)果為兩段
# str.rsplit() 作用是從右側(cè)開始分割
# 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為替換次數(shù),返回新字符串
print("s5.replace(\"a\",\"b\",3):{}".format(s5.replace("a", "b", 3)))

##str.strip([str]) 去除兩側(cè)的字符串, str.lstrip() 左側(cè)去除 str.rstrip() 右側(cè)去除
s6 = "abc xxx def"
print("s6.strip(\"abc\"):{}".format(s6.strip("abc")))

##拼接 直接用加號(hào)拼接


### 查
##索引,通過(guò)索引索引 str[i]

##切片 str[begin:end:step] 從begin到end 步長(zhǎng)為step,當(dāng)step為負(fù)數(shù)的時(shí)候表示從右到左,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() 查找字符串中子串出現(xiàn)的次數(shù)
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結(jié)尾,返回bool值
print("s6.endswith(\"abc\"):{}".format(s6.endswith("abc")))

##str.startswith(prefix[,start[,end]]) 判斷str是否以prefix結(jié)尾,返回bool值
print("s6.startswith(\"xyz\"):{}".format(s6.startswith("xyz")))

##str.isdigit() 判斷字符串是不是只有數(shù)字,返回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()))



####補(bǔ)充
# str是不可變對(duì)象

# r/R后接字符串表示字符串中所有字符都不做轉(zhuǎn)義
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

欄目分類
最近更新