標籤:ide ipython3 cat dex 針對 isa 文本 進階 swap
字串1,字串的定義
- 字串 就是 一串字元 ,是程式設計語言中表示文本的資料類型
- 在Python中可以使用一對雙引號
"
或者一對單引號‘
定義一個個字串
- 雖然可以是使用
\"
或者\‘
做字串的轉義,但是在實際開發中:
- 如果字串內部需要使用
"
,可以使用‘
定義字串
- 如果字串內部需要使用
‘
,可以使用"
定義字串
- 可以使用 索引 擷取一個字串中 指定位置的字元 ,索引技術從0開始
- 也可以使用
for
迴圈遍曆字串中每一個字元
大多數程式設計語言都是使用"
來定義字串
str_test = "Hello Python" for i in str_test: print(i) H e l l o P y t h o n # 1,統計字串長度 length = len(str_hello) print(length) 11 # 2,統計某一個小字串出現的次數 frequency_a = str_hello.count("llo") print(frequency_a) 2 # 如果收搜一個不存在的字串,則顯次數為0 frequency_b = str_hello.count("abc") print(frequency_b) 0 # 3,某一個字串出現的位置 location_a = str_hello.index("llo") print(location_a) 2 # 如果字串不存在,則程式報錯 location_b = str_hello.index("abc") print(location_b)
字串的常用操作
- 在
ipython3
中定義一個字串,例如str_hello = "hello hello"
- 輸入
str_hello.
按下TAB
鍵,ipython3
會提示字串 能夠使用的方法如下:
In [1]: str_hello = "hello hello" In [2]: str_hello. str_hello.capitalize str_hello.expandtabs str_hello.isalpha str_hello.isnumeric str_hello.ljust str_hello.rfind str_hello.split str_hello.translate str_hello.casefold str_hello.find str_hello.isascii str_hello.isprintable str_hello.lower str_hello.rindex str_hello.splitlines str_hello.upper str_hello.center str_hello.format str_hello.isdecimal str_hello.isspace str_hello.lstrip str_hello.rjust str_hello.startswith str_hello.zfill str_hello.count str_hello.format_map str_hello.isdigit str_hello.istitle str_hello.maketrans str_hello.rpartition str_hello.strip str_hello.encode str_hello.index str_hello.isidentifier str_hello.isupper str_hello.partition str_hello.rsplit str_hello.swapcase str_hello.endswith str_hello.isalnum str_hello.islower str_hello.join str_hello.replace str_hello.rstrip str_hello.title
提示:正是因為python內建提供的方法足夠多,才使得在開發時,能夠針對字串進行更加靈活的操作。
1,判斷類型
||||
Python 進階變數類型 --- 字串