python基礎之字串內建方法練習

2021-10-08 02:49:52 字數 2564 閱讀 4799

1.將字串 「abcd」 轉成大寫

2.計算字串 「cd」 在 字串 "abcd"**現的位置

3.字串 「a,b,c,d」 ,請用逗號分割字串,分割後的結果是什麼型別的?

4."喜歡".format(name=「李雷」) 執行會出錯,請修改**讓其正確執行

5.string = 「python is good」, 請將字串裡的python替換成 python,並輸出替換後的結果

6.有乙個字串 string = 「python修煉第一期.html」,請寫程式從這個字串裡獲得.html前面的部分,要用盡可能多的方式來做這個事情

7.如何獲取字串的長度?

9.「this is a book」, 請用程式判斷該字串是否以this開頭

11.「this is a book」, 請將字串裡的大寫字元轉成小寫字元

12.「this is a book」, 請將字串裡的小寫字元,轉成大寫字元

13.「this is a book\n」, 字串的末尾有乙個回車符,請將其刪除

#1

a ="abcd"

print

(a.upper(

))

abcd
#2

"abcd"

.find(

"cd"

)

2
#3

"a,b,c,d"

.split(

",")

['a', 'b', 'c', 'd']
"a.b.c.d"

.split(

".")

['a', 'b', 'c', 'd']
#4

"喜歡"

.format

(name=

"李雷"

,fruit=

"芒果"

)

'李雷喜歡芒果'
#5

string =

"python is good"

string.replace(

"python"

,"python"

)

'python is good'
#6

string =

"python修煉第一期.html"

print

(string[:11

])print

(string[0:

11])print

(string[-16

:-5]

)print

(string.split(

".")[0

])

python修煉第一期

python修煉第一期

python修煉第一期

python修煉第一期

#7

string =

"bianxia"

print

(len

(string)

)

7
#8

string =

'this is a book'

string.replace(

"book"

,)

#9

string =

"this is a book"

string.startswith(

"this"

)

true
#10

string =

"this is a book"

string.endswith(

)

false
#11

string =

"this is a book"

string.lower(

)

'this is a book'
#12

string =

"this is a book"

string.upper(

)

'this is a book'
#13

string =

"this is a book\n"

string.strip(

)

'this is a book'
參考了python基礎130道練習題

python之字串內建函式

本文詳細介紹python 常見的內建函式使用方法。1 string.capitalize 將字串的首字母進行大寫,其餘字元小寫。例 str hello world print str.capitaize hello world 2 center with,fillchar 返回乙個原字串居中,並使用...

python基礎之字串

1.單引號字串和轉義引號 在python中,字串是用單引號或者雙引號括起來,在表示字串的時候,單引號和雙引號有什麼區別嗎?事實上並沒有。在某些特殊情況時候,單引號和雙引號是不能換線交換的,比如在乙個字串中包含了雙引號,那麼這個字串就必須用單引號括起來,反之,乙個字串中包含了單引號,那麼這個字串就必須...

python基礎之字串

1.基本字串的操作 所有標準序列的操作 索引,分片,乘法,成員資格判斷,求長度,取最小和最大值 同樣適用,但是記住 字串都是不可變的。2.字串格式化 精簡版 字串的格式化可以使用字串格式化操作符 百分號 來實現。在 的左側放置乙個字串 格式化字串 而右側放置希望被格式化的值。可以使用乙個值,如乙個字...