字串常搭配使用函數

Da-Wei Chiang

大綱

  • 字串當list取值
  • count、index、in與not in、len、find
  • 大小寫英文字母轉換
  • 判斷英文字母
  • 其他常用判斷
  • 取代與分割
  • list轉換字串

字串當list取值

sting_Variable[index]
In [8]:
# 範例

word = "String To List"
print(word)
print(type(word))
print('字串當list使用')
print(word[0])
print(word[-1])
print(word[::2])
word[0] = "1"
print(word)
String To List
<class 'str'>
字串當list使用
S
t
Srn oLs
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-7e1fe5750b9e> in <module>
      8 print(word[-1])
      9 print(word[::2])
---> 10 word[0] = "1"
     11 print(word)

TypeError: 'str' object does not support item assignment
In [ ]:
## 請猜猜以下執行結果

word = "String To List"
word.append('~')
word.insert(0, '~')
print(word)

練習

  • 有一段字串如下
      This time is Python Class.
  • 請使用迴圈撰寫一支程式回答該字串中有幾個空白

count、index、in與not in、len、find

  • count(word) : word在字串中出現的次數
  • index(word) : word在字串中的索引值位置
  • len(word) : word的字元數
  • in與not in : 判斷字串中是否包含某文字, 回傳布林值
  • find(word) : 尋找word在字串中的位置, 若找不到則回傳-1
In [8]:
## 範例

word = "String To List"
print(' ' in word)
print(' ' not in word)
print(word.count(' '))
print(word.index(' '))
print(len(word))
print(word.find('to'))
True
False
2
6
14
-1

練習

  • 我有個字串如下,
This time is Python Class.
  • 請撰寫一隻程式回答以下問題
      1. 該字串中有空白嗎?
      1. 該字串中有幾個s字母

大小寫英文字母轉換

  • upper() : 全部轉大寫
  • lower() : 全部轉小寫
  • title() : 每個單字開頭轉大寫
  • capitalize() : 字串開頭第一個字轉大寫
In [68]:
## 範例

word = 'English String'
print('全部轉大寫', word.upper())
print('全部轉小寫', word.lower())
print('每個單字開頭轉大寫', word.title())
print('字串開頭第一個字轉大寫', word.capitalize())
全部轉大寫 ENGLISH STRING
全部轉小寫 english string
每個單字開頭轉大寫 English String
字串開頭第一個字轉大寫 English string

練習

  • 我有個字串如下,
This time is Python Class.
  • 請撰寫一隻程式回答以下問題
    • 該字串中有幾個t(大小寫都算)?
    • 將字串中每個單字開頭都改大寫並輸出

判斷英文字母

  • islower()
  • isupper()
  • istitle()
In [46]:
## 範例

word = '這是python課程9又4分之3堂課'
print(word.islower())
word = word.upper()
print(word)
print(word.isupper())
word = word.title()
print(word)
print(word.istitle())
True
這是PYTHON課程9又4分之3堂課
True
這是Python課程9又4分之3堂課
True

其他常用判斷

  • isalpha() : 判斷字串是否只有字母或中文字
  • isspace() : 判斷字串是否只有空白或換行字符、Tab字符
In [67]:
## 範例

print('---isalpha範例---')
word = ''
print(word, 'isalpha : ', word.isalpha())
word = ' '
print(word, 'isalpha : ', word.isalpha())
word = 'python'
print(word, 'isalpha : ', word.isalpha())
word = 'python class'
print(word, 'isalpha : ', word.isalpha())
word = 'python60hours'
print(word, 'isalpha : ', word.isalpha())
word = 'python課程'
print(word, 'isalpha : ', word.isalpha())
print('---isspace範例---')
word = ''
print(word, 'isspace : ', word.isspace())
word = ' '
print(word, 'isspace : ', word.isspace())
word = 'python'
print(word, 'isspace : ', word.isspace())
word = 'python class'
print(word, 'isspace : ', word.isspace())
word = '\n'
print(word, 'isspace : ', word.isspace())
word = '\t'
print(word, 'isspace : ', word.isspace())
---isalpha範例---
 isalpha :  False
  isalpha :  False
python isalpha :  True
python class isalpha :  False
python60hours isalpha :  False
python課程 isalpha :  True
---isspace範例---
 isspace :  False
  isspace :  True
python isspace :  False
python class isspace :  False

 isspace :  True
	 isspace :  True

練習

  • 請自行練習 isalphaisspace

取代與分割

  • split('word'): 預設空白分割或按照指定文字分割
  • replace('預取代文字' '取代文字word'): 將字串取代為你想要的文字
In [79]:
## 範例

print('---split範例---')
word = 'Python String split replace Example'
word_list = word.split()
print(word_list)
word = 'Today is Sunday. Tomorrow is blue Monday.'
word_list = word.split('.')
print(word_list)
print('---replace範例---')
word = 'Today is Sunday. Tomorrow is blue Monday.'
word = word.replace('.', '~~~')
print(word)
---split範例---
['Python', 'String', 'split', 'replace', 'Example']
['Today is Sunday', ' Tomorrow is blue Monday', '']
---replace範例---
Today is Sunday~~~ Tomorrow is blue Monday~~~

練習

  • 請自行練習splitreplace函數

list轉換字串

string_variable.join(list_variable)
In [11]:
## 範例

Example_List = ['one', 'two', 'three']
print(Example_List)
print(','.join(Example_List))
['one', 'two', 'three']
one,two,three

練習

  • 我有個字串如下, 請問我要如何在最前面加上「、後面加上」
    This time is Python Class.
  • 請加上「」後印出結果(「This time is Python Class.」)