python資料類型之一字串(str)

來源:互聯網
上載者:User

標籤:運行   字串分割   tor   seq   登入   world   ==   mini   pre   

字串是Python中最常用的資料類型之一,字串的本質是值,就像數字一樣

建立字串的方式也很簡單,只需為變數分配一個值即可

值的注意的是字串是不可變數,不能被修改

在python3中所有的字串均為Unicode字串

栗子:

var = ‘Hello World!‘

字串格式化

字串格式化使用字串的格式化操作符即%來實現

格式化字串的%s、%d、%.nf部分稱之為轉換說明符,標記了需要插入的轉換值的位置

%s:格式化的部分為字串str,如果不是字串可以強制類型轉換為字串

%d:格式化的部分為整數

%f:格式化的為浮點數,同時提供所需要保留的精度,一個句點加上需要保留的小數位元 

#%s預留位置,%d整數預留位置,%.nf浮點數,保留幾位小數name = input("請輸入你的姓名:")age = int(input("你的年齡是:"))money = float(input("你有多少零花錢捏?"))print("姓名是%s,年齡是%d,零花錢是%.2f"%(name,age,money))運行結果:D:\Python\Miniconda3\python.exe D:/Python/Python3.6.0/BestTest/BestTest/Recover/Day1.py請輸入你的姓名:xxxx你的年齡是:18你有多少零花錢捏?666.666姓名是xxxx,年齡是18,零花錢是666.67Process finished with exit code 0

字串方法

字串中還包含多種內建的方法,這是因為字串從string模組中繼承了很多方法

find()

find方法可以用來尋找一個較長字串中的子串

str1 = ‘to do or not to do ,this is a question‘#find方法用來尋找字串中的子串,並返回子串在最左端(第一次出現的位置)的下標print(str1.find(‘do’))#如果尋找的元素不存在,返回-1print(str1.find(‘xx’))運行結果:D:\Python\Miniconda3\python.exe 3-1Process finished with exit code 0#find還可以接收合始位置和結束位置參數,通過規定起始下標和結束下標,返回一定範圍內尋找的子串(顧頭不顧尾)str1 = ‘to do or not to do ,this is a question‘print(str1.find(‘do‘,5,20))運行結果:D:\Python\Miniconda3\python.exe16Process finished with exit code 0

join()                

join方法時非常重要的字串方法,用來串連list中的字串元素,列表中的元素必須是字串類型

join為split的逆方法

只能換種插入代碼的方式了,server502了,嗚嗚嗚嗚。。。。

#join為字串的方法,連接字串列表,為split的逆方法#如果列表中的元素不是字串則會報錯dir1 = [‘C:‘,‘adminstir‘,‘desktop‘]sep =‘\\‘url = sep.join(dir1)print(url)運行結果:D:\Python\Miniconda3\python.exe yC:\adminstor\desktopProcess finished with exit code 0************************#list中的內容不是字串list1 = [5,6]sep =‘+‘print(sep.join(list1))運行結果:D:\Python\Miniconda3\python.exe Traceback (most recent call last):  File "D:/Python/Python3.6.0/xxxxx/xxxxx/Recover/xxx.py", line 178, in <module>    print(sep.join(list1))TypeError: sequence item 0: expected str instance, int foundProcess finished with exit code 1

split()

split方法是join的逆方法,將字串按某個字元分割,返回為list的方式

#split為join的逆方法,將字串分割為list #‘\‘注意轉義url = ‘C:\\adminstor\desktop‘dir1 = url.split(‘\\‘)print(dir1)運行結果:D:\Python\Miniconda3\python.exe [‘C:‘, ‘adminstor‘, ‘desktop‘]Process finished with exit code 0****************************#當split中的內容不填寫時,為按照空格分割str1= ‘no zuo no die‘print(str1.split())運行結果:D:\Python\Miniconda3\python.exe [‘no‘, ‘zuo‘, ‘no‘, ‘die‘]Process finished with exit code 0

strip()

strip方法返回去除兩側(不包含中間)的空格或其他字元

#strip去除字串兩側(不包含中間)的空格或指定字串#strip()中的只能為空白或者為字串,預設為空白#去除字串首尾的空格str1 = ‘      username      ‘print(str1.strip())運行結果:D:\Python\Miniconda3\python.exe usernameProcess finished with exit code 0***************************************#去除字串首尾的指定字元str1 = ‘hahaha    username    hahaha‘print(str1.strip(‘hahaha‘))運行結果:D:\Python\Miniconda3\python.exe     username    Process finished with exit code 0

 lower()

返回字串的小寫字母

#lower返回字串的小寫字母#upper返回字串的大寫字母#小寫str1 = ‘Hello World!‘print(str1.lower())運行結果:D:\Python\Miniconda3\python.exe hello world!Process finished with exit code 0#大寫str1 = ‘Hello World!‘print(str1.upper())運行結果:D:\Python\Miniconda3\python.exe HELLO WORLD!Process finished with exit code 0
當在編寫不區分大小寫代碼時,就可以用上lower或者upper方法:例如輸入使用者名稱和密碼的時候忽略大小寫,只需要將儲存和填寫登入時的內容全部轉換為大寫或者小寫即可lower、upper方法還可以和strip方法一起連用,對比輸入和儲存的值
#upper()和strip()連用names = [‘DENNY‘, ‘JENNY‘,‘LIMING‘]Name = input(‘請輸入使用者名稱:‘)Name = (Name.strip()).upper()if Name in names:    print(‘使用者名稱已存在!‘)elif Name ==‘‘:    print(‘使用者名稱不可為空!‘)else:    print(‘請先註冊!‘)

  

replace()replace(‘oldstr’,‘newstr’)方法返回,某字串的所有匹配項均被替換後)得到的字串,類似於word文檔中的“尋找並替換”功能
str1 = ‘no zuo no die‘print(str1.replace(‘no‘,‘不‘))運行結果:D:\Python\Miniconda3\python.exe 不 zuo 不 dieProcess finished with exit code 0

  

 

python資料類型之一字串(str)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.