本文實(shí)例講述了Python學(xué)習(xí)筆記之字符串和字符串方法。分享給大家供大家參考,具體如下:
字符串
在 python 中,字符串的變量類型顯示為
str
。你可以使用雙引號(hào)
"
或單引號(hào)
'
定義字符串
定義字符串
my_string = 'this is a string!' my_string2 = "this is also a string!!!" # Also , we can use backslash '/' to escape quotes. this_string = 'Simon\'s skateboard is in the garage.' print(this_string)
字符串的常用操作
first_word = 'Hello' second_word = 'There' print(first_word + second_word) # HelloThere print(first_word + ' ' + second_word) # Hello There print(first_word * 5) # HelloHelloHelloHelloHello print(len(first_word)) # 5 print(first_word[0]) # H print(first_word[1]) # e
字符串[相關(guān)練習(xí)]
在字符串中正確的使用引號(hào)
ford_quote = 'Whether you think you can, or you think you can\'t--you\'re right.' print(ford_quote) # Whether you think you can, or you think you can't--you're right.
下面這段代碼的輸出是什么?
coconut_count = "34" mango_count = "15" tropical_fruit_count = coconut_count + mango_count print(tropical_fruit_count) # 3415 (并且 tropical_fruit_count 是字符串)
編寫服務(wù)器日志消息
username = "Kinari" timestamp = "04:50" url = "http://petshop.com/pets/mammals/cats" # TODO: print a log message using the variables above. The message should have the same format as this one: "Yogesh accessed the site http://petshop.com/pets/reptiles/pythons at 16:20." print(username + ' accessed the site ' + url + ' at ' + timestamp + '.')
使用字符串連接和
len
函數(shù)計(jì)算某些電影明星的實(shí)際完整姓名的長度
given_name = "William" middle_names = "Bradley" family_name = "Pitt" name_length = len(given_name + ' ' + middle_names + ' ' + family_name) # Now we check to make sure that the name fits within the driving license character limit driving_license_character_limit = 28 print(name_length <= driving_license_character_limit) # True
我們剛剛使用函數(shù)
len
計(jì)算出字符串的長度。當(dāng)我們向其提供整數(shù) 835 而不是字符串時(shí),函數(shù)
len
會(huì)返回什么?
Error
字符串方法
python 中的方法和函數(shù)相似,但是它針對(duì)的是你已經(jīng)創(chuàng)建的變量。方法特定于存儲(chǔ)在特定變量中的數(shù)據(jù)類型。
每個(gè)方法都接受字符串本身作為該方法的第一個(gè)參數(shù)。但是,它們還可以接收其他參數(shù)。我們來看看幾個(gè)示例的輸出。
my_string = "sebastian thrun" my_string.islower() # True my_string.count('a') # 2 my_string.find('a') # 3
可以看出,
count
和
find
方法都接受另一個(gè)參數(shù)。但是,
islower
方法不接受參數(shù)。如果我們要在變量中存儲(chǔ)浮點(diǎn)數(shù)、整數(shù)或其他類型的數(shù)據(jù),可用的方法可能完全不同!
字符串方法[相關(guān)練習(xí)]
-
對(duì)浮點(diǎn)型對(duì)象調(diào)用
islower
等方法會(huì)發(fā)生什么?例如13.37.islower()
-
會(huì)出現(xiàn)錯(cuò)誤, 方法
islower
屬于字符串方法,而不是浮點(diǎn)數(shù)方法。不同類型的對(duì)象具有特定于該類型的方法。例如,浮點(diǎn)數(shù)具有is_integer
方法,而字符串沒有。 - 練習(xí)字符串方法
my_name = "my name is Joh." cap = my_name.capitalize() print(cap) # My name is joh. ew = my_name.endswith('li') print(ew) # False ind = my_name.index('is') print(ind) # 8
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python列表(list)操作技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
