python中str用法_python數據類型之str用法

1、首字母大寫

語法:S.capitalize() ->str

title= "today is a good day"title_ca=title.capitalize()

print(title_ca)

結果:today is a good day

2、大寫轉小寫

1 語法:S.casefold() ->str2

3 title = "TODAY is a GOOD day"

4 title_ca =title.casefold()5 print(title_ca)

結果:Today is a good day

3、字符串居中

c = 'kong'ret= c.center(10,'*')

print(ret)

結果:***kong***

4、字符串子串數量統計

S.count(sub[, start[, end]]) -> inttitle= "today is a good day"title_ca= title.count('y',1,5)

print(title_ca)

結果:1

5、中文轉UTF-8

S.encode(encoding='utf-8', errors='strict') ->bytes

zw= '孔扎根'ut=zw.encode()

print(ut)

結果:b'\xe7\xa9\xba\xe6\x89\x8e\xe6\xa0\xb9'

6、字符串結束判斷

S.endswith(suffix[, start[, end]]) -> booltitle= "TODAY is a GOOD day"title_ca= title.endswith('day')

print(title_ca)

結果:True

7、TAB轉空格

S.expandtabs(tabsize=8) ->str

#默認是一個TAB轉8個空格

title= "TODAY\tis\ta\tGOOD\tday"title_ca=title.expandtabs()

print(title_ca)

結果:TODAYis a GOOD day

8、查找字符串的位置

S.find(sub[, start[, end]]) -> inttitle= "TODAY\tis\ta\tGOOD\tday"title_ca= title.find('s')

print(title_ca)

結果:7

9、字符串格式化

S.format(*args, **kwargs) ->str

#可傳入元組或字典

title= "{}\tis\ta\t{day_type}\tday"title_ca= title.format('TODAY',day_type='GOOD')

print(title_ca)

結果:TODAYis a GOOD day

10、字符串格式化,從字典輸入(format_map)

S.format_map(mapping) ->str

#輸入參數為字典,循環讀字典中的列表

maping_name={'name':['alex','join'],'age':[18,19]

}for x in range(2):

print('my name is {},and i is {} old'.format(maping_name['name'][x],maping_name['age'][x]))

結果:

my nameis alex,and i is 18old

my nameis join,and i is 19 old

11、字符串的索引位置

S.index(sub[, start[, end]]) -> int#查找Y的索引位置,從0開始數

title= "TODAY\tis\ta\tGOOD\tday"title_ca= title.index('Y')

print(title_ca)

結果:4

12、字符串中至少有一個數字

S.isalnum() -> bool#字符串不能有空格,否則失敗

title= "22TODAY"title_ca=title.isalnum()

print(title_ca)

結果:True

13、字符串中至少有一個字母

S.isalpha() -> bool#字符串不能有空格或TAB鍵

title= "22TODAY"title_ca=title.isalnum()

print(title_ca)

結果:True

14、字符串是否為數值

num = "1"#unioncode

print(num.isdigit())

print(num.isdecimal())

print(num.isnumeric())

結果:

True

True

True

num= "1"#全角

print(num.isdigit())

print(num.isdecimal())

print(num.isnumeric())

結果:

True

True

True

num= b"1"#byteprint(num.isdigit())

print(num.isdecimal())

print(num.isnumeric())

結果:

True

AttributeError:'bytes' object has no attribute 'isdecimal'AttributeError:'bytes' object has no attribute 'isnumeric'num= "IV"#羅馬字符

print(num.isdigit())

print(num.isdecimal())

print(num.isnumeric())

結果:

False

False

False

num= "四"#漢字

print(num.isdigit())

print(num.isdecimal())

print(num.isnumeric())

結果:

False

False

True

支持的字符:

isdigit:支持 unioncode,全角,byte,漢字

isdecimal:支持 unioncode,全角,

isnumeric:支持 unioncode,全角,漢字

報錯:

isdigit不會報錯,后兩種在byte判斷時會報錯

15、判斷字符串是否為有效標識符(可做為函數名稱)

#S.isidentifier() -> boolt_code= 'test'print(t_code.isidentifier())

結果:返回True

t_code= '23test'print(t_code.isidentifier())

結果:返回False

16、判斷字符串是否全小寫

#S.islower() -> boolt_code= 'kongzhagen'print(t_code.islower())

結果:返回True

t_code= 'kongzHagen'print(t_code.islower())

結果:返回False

17、判斷字符串是否全整型數字

#S.isnumeric() -> boolt_code= '123'print(t_code.isnumeric())

結果:返回True

t_code= '123d'print(t_code.isnumeric())

結果:返回False

t_code= '123.123'print(t_code.isnumeric())

結果:返回False

18、判斷所有字符是否可打印

#S.isprintable() -> boolt_code= '123KKK'print(t_code.isprintable())

返回:True

t_code= ''print(t_code.isprintable())

返回:True

t_code= 'KKK\n\t'print(t_code.isprintable())

返回:False

19、判斷字符中是否全為空格

#S.isspace() -> boolt_code= ' 'print(t_code.isspace())

結果:返回True

t_code= '123 KKK'print(t_code.isspace())

結果:返回False

20、判斷字符串是否為標題格式(首字母大寫)

#S.istitle() -> bool

t_code = 'Today Is A Good Day'

print(t_code.istitle())

結果:True

t_code = 'Today Is A Good day'

print(t_code.istitle())

結果:False

t_code = 'TODAY IS'

print(t_code.istitle())

結果:False

21、判斷字符串是否全大寫

#S.isupper() -> boolt_code= 'Today Is A Good day'print(t_code.isupper())

結果:False

t_code= 'TODAY IS'print(t_code.isupper())

結果:True

22、字符串連接

#S.join(iterable) ->str

t_code= 'Today Is A Good Day't1_code= '.'.join(t_code)

print(t1_code)

結果:T.o.d.a.y. .I.s. .A. .G.o.o.d. .D.a.y

23、左對齊,達不到指定長度,右則填充

#S.ljust(width[, fillchar]) ->str

t_code= 'Today Is A Good Day'print(t_code.ljust(22,'*'))

結果:Today Is A Good Day***

24、轉小寫

#S.lower() ->str

t_code= 'Today Is A Good Day'print(t_code.lower())

結果:todayis a good day

25、左邊去除指定字符,默認為空格

# S.lstrip([chars]) ->str

t_code= 'Today Is A Good Day'print(t_code.lstrip('T'))

結果:oday Is A Good Day

t_code= 'Today Is A Good Day'print(t_code.lstrip())

結果:Today Is A Good Day

26、

maketrans

27、partition

按指定的字符拆分字符串,分頭、分隔串、尾,未找到指定的分隔符,頭返回自己,后面兩個返回空

#S.partition(sep) ->(head, sep, tail)

t_code= 'TodayIs A Good Day'print(t_code.partition('a'))

結果:('Tod', 'a', 'yIs A Good Day')

print(t_code.partition('P'))

結果:('TodayIs A Good Day', '', '')

28、replace:字符串替換

將老字符串替換為新字符串,可指定替換次數

#S.replace(old, new[, count]) ->str

t_code= 'TodayIs A Good Day,To today'print(t_code.replace('T','M',2))

結果:ModayIs A Good Day,Mo today

29、rfind:返回查詢到的字符串的最大索引

#S.rfind(sub[, start[, end]]) -> int

t_code = 'TodayIs A Good Day,To today'print(t_code.rfind('d'))

結果:24

30、rindex:類似rfind,但如果沒找到會報錯

#S.rindex(sub[, start[, end]]) -> int

t_code = 'TodayIs A Good Day,To today'

print(t_code.rindex('p'))

結果:

Traceback (most recent call last):

File "C:/51py/day1/study.py", line 90, in

print(t_code.rindex('p'))

ValueError: substring not found

31、rjust:右對齊,左側填充字符

#S.rjust(width[, fillchar]) -> str

t_code = 'Today'print(t_code.rjust(10,'*'))

結果:*****Today

32、rpartition:類似partition,如果未找到字符串,則空值在左邊

#S.rpartition(sep) ->(head, sep, tail)

t_code= 'Today is a good day'print(t_code.rpartition('isa'))

結果:('', '', 'Today is a good day')

33、rsplit:分割字符串,從右邊開始

#S.rsplit(sep=None, maxsplit=-1) ->list of strings

t_code= 'Today is a good day'print(t_code.rsplit('o',1))

結果:['Today is a go', 'd day']

34、rstrip:右邊去空格

#S.rstrip([chars]) ->str

t_code= 'Today is a good day'print(t_code.rstrip())

結果: Todayis a good day

35、splitlines:方法返回一個字符串的所有行列表,可選包括換行符的列表(如果num提供,則為true)

#S.splitlines([keepends]) ->list of strings

t_code= 'Today\n is\n a\n good\n day'print(t_code.splitlines())

print(t_code.splitlines(0))

print(t_code.splitlines(1))

結果:

['Today', 'is', 'a', 'good', 'day']

['Today', 'is', 'a', 'good', 'day']

['Today\n', 'is\n', 'a\n', 'good\n', 'day']

36、startswith:如果字符串以指定的字符為前綴,則返回true,否則返回false

#S.startswith(prefix[, start[, end]]) -> boolt_code= 'Today\n is\n a\n good\n day'print(t_code.startswith('Today'))

結果:True

37、strip:去除字符串前后的空格

#S.strip([chars]) ->str

t_code= 'Today\n is\n a\n good\n day'print(t_code.strip())

結果:

Todayisa

good

day

38、swapcase:大小寫互轉

#S.swapcase() -> str

t_code = ' Today Is a Good Day '

print(t_code.swapcase())

結果: tODAY iS A gOOD dAY

39、title:返回的字符串為title格式,首字母大寫

#S.title() ->str

t_code= 'today is a Good Day'print(t_code.title())

結果:Today Is A Good Day

40、maketrans:用于創建字符映射的轉換表,兩個參數為長度相等的字符串

#B.maketrans(frm, to) ->translation table

intab= "aeiou"outab= "1234k"trantab=t_code.maketrans(intab,outab)

print(trantab)

結果:{97: 49, 111: 52, 117: 107, 101: 50, 105: 51}

41、translate:根據參數table給出的表轉換字符中的字符

# S.translate(table) ->str

t_code= 'today is a Good Day'trantab= {97:49}

print(t_code.translate(trantab))

結果: tod1yis 1 Good D1y

42、ord與chr是配對函數

>>> chr(65)'A'

>>> ord('A')65

43、upper:將字符串轉為大寫

#S.upper() ->str

t_code= 'today is a Good Day'print(t_code.upper())

結果:TODAY IS A GOOD DAY

44、zfill:數字填零

#S.zfill(width) ->str

t_code= '123'print(t_code.zfill(5))

結果:00123

45、匯總

str = 'https://www.baidu. com234'# print(str.capitalize()) # 第一個字母大寫

# print(str.count('w')) # 字符在字符串中出現的次數

# print(str.endswith('com')) # 字符串是否以com結尾

# print(str.expandtabs(tabsize=2)) # 字符串中的tab轉為兩個空格

# print(str.find('bai')) # 返回字符串中bai的索引

# print(str.rfind('bai')) # 返回字符串中bai的索引,從右邊找

# print(str.index('bai')) # 返回字符串中bai的索引,未找到會報錯

# print(str.rindex()) # 返回字符串中bai的索引,未找到會報錯(從右邊找)

# print(str.isalnum()) # 如果所有字符都是數字或字母,則返回True

# print(str.isalpha()) # 如果所有字符都是字母,則返回True

# print(str.isnumeric()) # 如果所有字符都是數字,則返回T

# print(str.isdecimal()) # 可解釋為十進制數,則返回True

# print(str.isdigit()) # 可解釋為數字,則返回True

# print(str.islower()) # 字符串中的字母都小寫,則返回True(可以有其它字符)

# print(str.isupper()) # 字符串中的字母都大寫,則返回True(可以有其它字符)

# print(str.isspace()) # 字符串中全是空格,則返回True

# print(str.istitle()) # 如果字符串是標題化的,則返回True(每個單詞首字母大寫,其它小寫)

# print(str.ljust(100)) # 字符串左對齊,長度100

# print(str.rjust(100)) # 字符串右對齊,長度100

# print(str.lower()) # 所有字符轉小寫

# print(str.lstrip()) # 去掉字符串左邊的空格

print str.replace('t','2',2) # 將p 替換為2,替換2次

print str.rfind('o') # 從右邊查找第一個o所在的位置

print str.rindex('o') # 從右邊查找第一個o所在的位置的索引

print str.partition('du') # 從左邊找到第一個du,并以之分隔字符串,返回列表

print str.rstrip() # 去掉右邊的空格

print str.split('w',2) # 以w為分隔符,切分字符串

print str.splitlines() # 以行做為分隔符

print str.startswith('http') # 是否以http開頭

print str.swapcase() # 翻轉大小寫

print str.title() # 將string標題化,所有單詞首字母大寫,其它小寫

print str.upper() # 所有字母大寫

print str.zfill(150) # 返回150個字符,原字符串右對齊,前面填充0

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/260780.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/260780.shtml
英文地址,請注明出處:http://en.pswp.cn/news/260780.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

WPF 窗體設置

WPF 當窗體最大化時控件位置的大小調整&#xff1a; View Code 1 <Window x:Class"WpfApplication1.MainWindow"2 xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"3 xmlns:x"http://schemas.microsoft.com/wi…

Git實踐

Git是什么自不必說。Git和gitlab安裝和實踐在后邊的倆篇中會寫。本篇僅重點寫Git自動部署。Git同樣有Hooks,可以用于各種需求。可以控制提交commit名稱&#xff0c;可以控制代碼規范&#xff0c;也當然包含以下要介紹的自動部署&#xff0c;也不僅包含這些。Git自動部署簡單的思…

第3章 Python 數字圖像處理(DIP) - 灰度變換與空間濾波14 - 平滑低通濾波器 -高斯濾波器核的生成方法

目錄平滑&#xff08;低通&#xff09;空間濾波器低通高斯濾波器核統計排序&#xff08;非線性&#xff09;濾波器平滑&#xff08;低通&#xff09;空間濾波器 平滑&#xff08;也稱平均&#xff09;空間濾波器用于降低灰度的急劇過渡 在圖像重取樣之前平滑圖像以減少混淆用…

易經0

--- 陽爻 - - 陰爻 從下往上 畫爻 (yao) 三畫卦 --> 2^38 (八卦) 那天有空用程序 解析一下 六畫卦 --> 2^664(卦) 卦形記憶歌&#xff1a;宋代朱熹的《周易本義》寫了《八卦取象歌》幫人記卦形&#xff1a; 乾三連&#xff0c;坤六斷&#xff1b;震仰盂&#xff0c;艮覆碗…

python3.7怎么安裝turtle_python怎么安裝turtle

turtle庫是Python語言中一個很流行的繪制圖像的函數庫&#xff0c;想象一個小烏龜&#xff0c;在一個橫軸為x、縱軸為y的坐標系原點&#xff0c;(0,0)位置開始&#xff0c;它根據一組函數指令的控制&#xff0c;在這個平面坐標系中移動&#xff0c;從而在它爬行的路徑上繪制了圖…

強制html元素不隨窗口縮小而換行

<style> div{ white-space:nowrap; } </style> 強制div內的元素不隨窗口縮小而換行 本文出自 “點滴積累” 博客&#xff0c;請務必保留此出處http://tianxingzhe.blog.51cto.com/3390077/1679366

靜態變量、方法

static 變量---所有對象共享一個變量&#xff08;全局變量區&#xff09;&#xff0c;無需構造---概念上和.net相同所有對象共享一個變量的實質&#xff1a;聲明時&#xff1a;堆區存放一個地址&#xff0c;地址指向全局變量區。然后當類對象聲明時&#xff0c;只是在堆區中為自…

python語言是機器語言_Python解釋器:源代碼--字節碼--機器語言

"一個用編譯性語言比如C或C寫的程序可以從源文件&#xff08;即C或C語言&#xff09;轉換到一個你的計算機使用的語言&#xff08;二進制代碼&#xff0c;即0和1&#xff09;。這個過程通過編譯器和不同的標記、選項完成。當你運行你的程序的時候&#xff0c;連接/轉載器軟…

第3章 Python 數字圖像處理(DIP) - 灰度變換與空間濾波15 - 銳化高通濾波器 -拉普拉斯核(二階導數)

目錄銳化&#xff08;高通&#xff09;空間濾波器基礎 - 一階導數和二階導數的銳化濾波器二階導數銳化圖像--拉普拉斯銳化&#xff08;高通&#xff09;空間濾波器 平滑通過稱為低通濾波類似于積分運算銳化通常稱為高通濾波微分運算高過&#xff08;負責細節的&#xff09;高頻…

Debian on VirtualBox下共享win7文件夾設置

借用&#xff1a;http://www.dbasoul.com/2010/695.html 1. 安裝增強功能包(Guest Additions) 參考文檔&#xff1a;Debian下安裝VirtualBox增強功能2. 設置共享文件夾 重啟完成后點擊”設備(Devices)” -> 共享文件夾(Shared Folders)菜單&#xff0c;添加一個共享文件夾&a…

第四周作業

1、復制/etc/skel目錄為/home/tuser1&#xff0c;要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。cp -r /etc/skel/ /home/tuser1/chmod -R go--- /home/tuser1/2、編輯/etc/group文件&#xff0c;添加組hadoop。vim /etc/group G, o, hadoop:x:501, esc, …

C# 導出 Excel 數字列出現‘0’的解決辦法

在DataGird的中某一列全是數字并且長度大于15的字符&#xff0c;在導出excel時數字列第15-18位全部為0。解決辦法&#xff1a;在需導出數字列前加入英文字符狀態的單引號&#xff08;‘ &#xff09;&#xff0c;如&#xff1a;<asp:TemplateField HeaderText"身份證號…

在python是什么意思_python 的 表示什么

python代碼里經常會需要用到各種各樣的運算符&#xff0c;這里我將要和大家介紹的是Python中的&&#xff0c;想知道他是什么意思嗎&#xff1f;那就和小編一起來了解一下吧。&是位運算符-與&#xff0c;類似的還有|&#xff08;或&#xff09;&#xff0c;!(非)。 整數…

Ubuntu 更改ROOT密碼的方法

真蛋疼啊&#xff0c;剛安裝了Ubuntu 需要安裝程序&#xff0c;提示輸入root密碼&#xff0c;我才想起來Ubuntu的root密碼是什么&#xff0c;我貌似沒設置啊。 上網搜索了下相關信息&#xff0c;才知道原來root默認是沒有密碼的。 需要使用以下命令 sudo passwd root 然后會要求…

DevExpress控件GridControl中的布局詳解 【轉】

DevExpress控件GridControl中的布局詳解 【轉】 2012-10-24 13:27:28| 分類&#xff1a; devexpress | 標簽&#xff1a;devexpress |舉報|字號 訂閱 http://www.cnblogs.com/martintuan/archive/2011/03/05/1971472.html 進行DevExpress控件GridControl的使用時&#xff…

第3章 Python 數字圖像處理(DIP) - 灰度變換與空間濾波16 - 銳化高通濾波器 - 鈍化掩蔽和高提升濾波

目錄銳化&#xff08;高通&#xff09;空間濾波器鈍化掩蔽和高提升濾波銳化&#xff08;高通&#xff09;空間濾波器 平滑通過稱為低通濾波類似于積分運算銳化通常稱為高通濾波微分運算高過&#xff08;負責細節的&#xff09;高頻&#xff0c;衰減或抑制低頻 鈍化掩蔽和高提…

python畫圓并填充圖形顏色_如何使用python設計語言graphics繪制圓形圖形

在python設計語言中&#xff0c;可以利用第三方包graphics繪制不同的圖形&#xff0c;有圓形、直線、矩形等。如果想要繪制一個圓形&#xff0c;可以設置圓形的半徑和坐標位置。下面利用一個實例說明繪制圓形&#xff0c;操作如下&#xff1a;工具/原料 python 截圖工具 方法/步…

設計模式學習-工廠方法模式

在上文(設計模式學習-簡單工廠模式)的模擬場景中&#xff0c;我們用簡單工廠模式實現了VISA和MASTERARD卡的刷卡處理&#xff0c;系統成功上線并運行良好&#xff0c;突然有一天老大跑來說&#xff0c;我們的系統需要升級&#xff0c;提供對一般銀聯卡的支持。怎么辦&#xff1…

3ds Max Shortcuts 快捷鍵大全

主界面 【Q】選擇循環改變方式 【W】移動 【E】旋轉 【R】縮放循環改變方式 【7】物體面數 【8】Environment 【9】Advanced lighting 【0】Render to Textures 【1】【2】【3】【4】【5】分別對應5個次物體級別&#xff0c;例如Edit Mesh中的點、線、面、多邊形、 【F2】切換在…