日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python学习:字符串

發布時間:2024/9/30 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python学习:字符串 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、字符串創建

我們?般使?引號來創建字符串。創建字符串很簡單,只要為變量分配?個值即可。

a = 'hello world' print(type(a)) name1 = """ Rose """ name2= ''' Tom '''#三引號形式的字符串?持換?。

二、字符串輸入輸出

(1)字符串輸出

name = 'Tom' print('我的名字是%s' % name)

(2)字符串輸入

name = input('請輸?您的名字:') print(f'您輸?的名字是{name}') print(type(name))

三、下標

“下標” ?叫 “索引” ,就是編號。下標從0開始

name = "abcdef" print(name[1]) print(name[0]) print(name[2])#輸出為b a c

四、切片

切?是指對操作的對象截取其中?部分的操作。字符串、列表、元組都?持切?操作。
格式為:
序列[開始位置下標:結束位置下標:步?]
(1)不包含結束位置下標對應的數據, 正負整數均可;
(2) 步?是選取間隔,正負整數均可,默認步?為1

name = "abcdefg" print(name[2:5:1]) # cde print(name[2:5]) # cde print(name[:5]) # abcde print(name[1:]) # bcdefg print(name[:]) # abcdefg print(name[::2]) # aceg print(name[:-1]) # abcdef,1表示倒數第?個數據 print(name[-4:-1]) # def print(name[::-1]) # gfedcba

五、常用操作方法

(1)find():檢測某個?串是否包含在這個字符串中,如果在返回這個?串開始的位置下標,否則則返回-1。

str = "hello world and itcast and itheima and Python" print(str.find('and')) # 12

(2)index():檢測某個?串是否包含在這個字符串中,如果包含則返回這個?串開始的位置下標,否則報異常。

str = "hello world and itcast and itheima and Python" print(str.index('and')) # 12 print(str.index('ands')) # 報錯

(3)rfind(): 和find()功能相同,但查找?向為右側開始。
(4)rindex():和index()功能相同,但查找?向為右側開始。
(5)count():返回某個?串在字符串中出現的次數

str = "hello world and itcast and itheima and Python" print(str.count('and')) # 3 print(str.count('ands')) # 0 print(str.count('and', 0, 20)) # 1

(6)replace():替換,替換次數如果超出?串出現次數,則替換次數為該?串出現次數。

str = "hello world and itcast and itheima and Python" # 結果:hello world he itcast he itheima he Python print(str.replace('and', 'he')) # 結果:hello world he itcast he itheima he Python print(str.replace('and', 'he', 10)) # 結果:hello world and itcast and itheima and Python print(str)

注:數據按照是否能直接修改分為可變類型和不可變類型兩種。字符串類型的數據修改的時候不能改變原有字符串,屬于不可變類型。
(7)split():按照指定字符分割字符串。
格式為:字符串序列.split(分割字符, num) ,
num表示的是分割字符出現的次數,即將來返回數據個數為num+1個。

str = "hello world and itcast and itheima and Python" # 結果:['hello world ', ' itcast ', ' itheima ', ' Python'] print(str.split('and')) # 結果:['hello world ', ' itcast ', ' itheima and Python'] print(str.split('and', 2)) # 結果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python'] print(str.split(' ')) # 結果:['hello', 'world', 'and itcast and itheima and Python'] print(str.split(' ', 2))

(8)join():??個字符或?串合并字符串,即將多個字符串合并為?個新的字符串。

list = ['c', 'z', 'b', 'k'] t1 = ('a', 'b', 'c', 'd') # 結果:c_z_b_k print('_'.join(list)) # 結果:a...b...c...d print('...'.join(t1))

(9)capitalize():將字符串第?個字符轉換成?寫。

str = "hello world " # 結果:Hello world print(str.capitalize())#capitalize()函數轉換后,只字符串第?個字符?寫,其他的字符全都?寫。

(10)title():將字符串每個單詞?字?轉換成?寫。

str = "hello world " # 結果:Hello World print(str.title())

(11)lower():將字符串中?寫轉?寫。

str = "Hello World" # 結果:hello world print(mystr.lower())

(12)upper():將字符串中?寫轉?寫。

str = "hello world " # 結果:HELLO WORLD print(mystr.upper())

(13)lstrip():刪除字符串左側空?字符。
(14)rstrip():刪除字符串右側空?字符。
(15)strip():刪除字符串兩側空?字符。
(16)ljust():返回?個原字符串左對?,并使?指定字符(默認空格)填充?對應?度 的新字符串。
格式為:

字符串序列.ljust(?度, 填充字符)

(17)rjust():返回?個原字符串右對?,并使?指定字符(默認空格)填充?對應?度 的新字符串,格式和ljust()相同。
(18)center():返回?個原字符串居中對?,并使?指定字符(默認空格)填充?對應?度 的新字符串,格式和ljust()相同。
(19)startswith():檢查字符串是否是以指定?串開頭,是則返回 True,否則返回 False。如果設置開始和結束位置下標,則在指定范圍內檢查。
格式為:

字符串序列.startswith(?串, 開始位置下標, 結束位置下標) str = "hello world" # 結果:True print(mystr.startswith('hello')) # 結果False print(mystr.startswith('hello', 5, 20))

(20)endswith()::檢查字符串是否是以指定?串結尾,是則返回 True,否則返回 False。如果設置開始和結束位置下標,則在指定范圍內檢查。
格式為:

字符串序列.endswith(?串, 開始位置下標, 結束位置下標)

(21)isalpha():如果字符串?少有?個字符并且所有字符都是字?則返回 True, 否則返回 False。

mystr1 = 'hello' mystr2 = 'hello111' # 結果:True print(mystr1.isalpha()) # 結果:False print(mystr2.isalpha())

(22)isdigit():如果字符串只包含數字則返回 True 否則返回 False。

mystr1 = 'a12' mystr2 = '12' # 結果: False print(mystr1.isdigit()) # 結果:False print(mystr2.isdigit())

(23)isalnum():如果字符串?少有?個字符并且所有字符都是字?或數字則返 回 True,否則返回False。

str1 = 'aaa12345' str2 = '12345-' # 結果:True print(mystr1.isalnum()) # 結果:False print(mystr2.isalnum())

(24)isspace():如果字符串中只包含空?,則返回 True,否則返回 False。

str1 = '1 2 3' str2 = ' ' # 結果:False print(str1.isspace()) # 結果:True print(str2.isspace())

總結

以上是生活随笔為你收集整理的Python学习:字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。