溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python學(xué)習(xí)筆記之字符串和字符串方法實(shí)例詳解

發(fā)布時(shí)間:2020-09-21 17:38:02 來源:腳本之家 閱讀:121 作者:Johnny丶me 欄目:開發(fā)技術(shù)

本文實(shí)例講述了Python學(xué)習(xí)筆記之字符串和字符串方法。分享給大家供大家參考,具體如下:

字符串

在 python 中,字符串的變量類型顯示為 str。你可以使用雙引號 " 或單引號 ' 定義字符串

定義字符串

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í)]

在字符串中正確的使用引號

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ù)相似,但是它針對的是你已經(jīng)創(chuàng)建的變量。方法特定于存儲在特定變量中的數(shù)據(jù)類型。

Python學(xué)習(xí)筆記之字符串和字符串方法實(shí)例詳解
注:圖片來源網(wǎng)絡(luò)

每個(gè)方法都接受字符串本身作為該方法的第一個(gè)參數(shù)。但是,它們還可以接收其他參數(shù)。我們來看看幾個(gè)示例的輸出。

my_string = "sebastian thrun"
my_string.islower() # True
my_string.count('a') # 2
my_string.find('a') # 3

可以看出,countfind 方法都接受另一個(gè)參數(shù)。但是,islower 方法不接受參數(shù)。如果我們要在變量中存儲浮點(diǎn)數(shù)、整數(shù)或其他類型的數(shù)據(jù),可用的方法可能完全不同!

字符串方法[相關(guān)練習(xí)]

  • 對浮點(diǎn)型對象調(diào)用 islower 等方法會(huì)發(fā)生什么?例如 13.37.islower()
  • 會(huì)出現(xiàn)錯(cuò)誤, 方法 islower 屬于字符串方法,而不是浮點(diǎn)數(shù)方法。不同類型的對象具有特定于該類型的方法。例如,浮點(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)典教程》

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI