認識字串

2021-09-26 07:47:47 字數 3810 閱讀 2250

字串是python中常用的資料型別。我們一般使用引號來建立字串。建立字串很簡單,只要為變數分配乙個值即可。

a = 'hello world'

b ="abcdefg"

print(type(a))

print(type(b))

name1 = 'tom'

name2 = "rose"

name3 = '''tom'''

name4 = '''rose'''

a ='''i am tom,nice to meet you!'''

b = "''" i am rose,nice to meet you!"""

注意:三引號形式的字串支援換行。

print('hello world')

name = 'tom'

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

print(f'我的名字是')

在python中,使用input()接收使用者輸入

name =input('請輸入您的名字:')

print(f'您輸入的名字是')

print(type(name))

「下標又叫「索引」」,就是編號。比如火車座位號,座位號,座位號的作用:按照編號快速查詢到對應的座位。同理,下標的作用是通過下標快速找到對應的資料。

name = "abcdef"

print(name[0])

切片是指對操作的物件擷取其中一部分的操作。字串,列表,元組都支援切片操作。

序列【開始位置下標:結束位置下標:步長】
1.不包含結束位置下標對應的資料,正負整數均可

2.步長是選取時間間隔,正整數均可,預設步長為1

3.如果選取方向步長方向相反則不能列印

name ="abcdefg"

print (name[2:5:1]) #cde

print(name[2:5]) #cde

print(name【:5】) #abcde

字串的常用操作方法有查詢,修改和判斷三大類。

所謂字串查詢方法即是查詢子串在字串中的位或出現的次數。

mystr = ''hello word and itcast and itheima and python"

print(mystr.find('and')) #12

print(mystr.find('and',15,30)) #23

print(mystr.find('ands')) # -1

#find

#index

#count

#rfind

#rindex

mystr = "hello world and itcast and itheima and python"

#print(mystr.find('and'))

#print(mystr.find('ands'))

#print(mystr.find('and',15,30))

#print(mystr.index('and'))

# print(mystr.index('ands'))

# print(mystr.index('and',15,30))

#print(mystr.count('and'))

# print(mystr.counr('ands'))

#print(mystr.count('ands',15,30))

#print(mystr.rfind('and'))

#print(mystr.rfind('ands'))

print(mystr.rindex('and',15,30))

所謂修改字串,指的就是通過函式的形式修改字串中的資料。

-replace():替換

1.語法

字串序列.replace(舊子串,新子串,替換次數)
注意:替換次數如果查出子串出現次數,則替換次數為該子串出現次數。

2.快速體驗

mystr = "hello word and itcast and itheima and python"

# new_str = mystr.replace('and','he')

# print(mystr)

# print(new_str)

# -- 說明 字串是不可變型別

# new_str = mystr.replace('and','he',1)

# print(new_str)

字串序列.split(分割字元,num)
注意:num表示的是分割字元出現的次數,即將來返回資料個數為num+1個

2.快速體驗

mystr = "hello word and itcast and itheima and python"

new_str = mystr.replace('and','he',1)

print(new_str)

1.語法

字元或字串.join(多字串組成的序列)
2.快速體驗

#合併字元為乙個大字串

mylist = ['aa','bb','cc']

new_str = "...".join(mylist)

print(new_str)

mystr = "hello world and itcast and itheima and python"

print(mystr.capitalize())

注意:capitalize()函式轉換後,只字串第乙個字元大寫,其他的字元全部小寫。

mystr = 'hello world i am python'

# new_str = mystr.capitalize()

# new_str = mystr.title()

# new_str =mystr.upper()

new_str = mystr.lower()

print(new_str)

mystr = 'hello world i am python'

print(mystr)

# new_str = mystr.strip()

#new_str = mystr.rstrip()

# print(new_str)

new_str =mystr.strip()

print(new_str)

所謂判斷即是判斷真假,返回的結果是布林型資料型別:true或false.

-startswith(): 檢查字串是否是以指定字串開頭,是則返回true,否則返回false。如果設定開始和結束位置下標,則在指定範圍檢查。

1.語法

字串序列.startswith(子串,開始位置下標,結束位置下標)
2.快速體驗

mystr = ''hello world and itcast and itheima and python "

結果:true

print(mystar.startswith('hello'))

結果:false

Python字串詳解 1 認識字串

一 認識字串的幾種寫法 1.普通寫法 1.單引號 a hello world 2.雙引號 b tom 3.三引號 單引號 c i am tom 4.三引號 雙引號 d i am tom 2.段落寫法 有些時候,字串是乙個段落,如果放在一行,閱讀 會很吃力,所以這時候需要換行寫 1.三引號 單引號 a...

Python學習之路10 認識字串

字串是最常用的資料型別,一般使用引號來建立。我們可以先來乙個例子來看看,以前學習過通過使用type來檢測 的資料型別,可見 hello world 是 str 型別,即字串型別。a hello world print a print type a 輸出的是 hello world 如下所示,三種都是...

初識字串

什麼是字串?字串就是有限個字元的序列,比如 this is a string 關於字串的表達方式,有三種,包括 以三個雙引號或單引號開頭的字串可以折行 比如 s3 可以隨便跨行,通過使用 是轉義符,用來表達原本的意思,比如 print 也可以通過 八進位制或者十六進製制數以及unicode編碼來表示...