Allen Chou
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: String type: slide tags: 資芽簡報 --- # String 2023 資訊之芽北區 Py 班 感謝去年講師熊老師育霆讓我~~抄襲~~修改他的簡報 --- ## Recall: what are strings? 我們來複習第一週教過的東西 ---- 一段文字 ```python= print(type("Hello, world!")) # <class 'str'> ``` ---- 可以用單引號或雙引號包起來 ```python= "I love Python" # 用雙引號包起來 'I love Python' # 用單引號包起來 ``` ---- 加法(串接)、乘法(重複接) ```python= name = "Madoka" print("Hello, " + name) # Hello, Madoka print(name * 3) # MadokaMadokaMadoka ``` ---- 如果要用引號怎麼辦 ```python= print('Hello. It's me.') ``` ``` File "<stdin>", line 1 print('Hello. It's me.') ^^^^^^^^^^^^ SyntaxError: invalid syntax. Perhaps you forgot a comma? ``` ---- 跳脫字元,例如 `\n`, `\'` ```python= print('Hello. It\'s me.') # Hello. It's me. print('Never gonna give you up', 'Never gonna let you down', 'Never gonna run around and desert you', sep='\n') """Never gonna give you up Never gonna let you down Never gonna run around and desert you """ ``` ---- 多行字串 ```python= print("""這幾行 會被 印出來 :)""") ``` ---- f-string ```python= name = "Kira Yoshikage" age = 33 print("name=", name, "age=", age) # name= Kira Yoshikage age= 33 print(f"name={name} age={age}") # name=Kira Yoshikage age=33 print(f"{name=} {age=}") # name='Kira Yoshikage' age=33 ``` --- ## 名詞解釋:字元 char 字元:一個字 ```python= 'a' 'b' 'c' ``` 字串:由一串字元組成 ```python= 'a' + 'b' + 'c' ``` ---- ## 補充:Python 沒有 char 在 C, C++, Java 等程式語言中有 char (字元) 資料型別,但在 Python 中沒有 char 資料型別,只有長度為 1 的字串。 ```c= char c = 'a'; // C and C++ ``` ```python= c = 'a' # Python # is equivalent to c = "a" print(type(c)) ``` --- ## 跟 `list` 差不多一樣的地方 在許多語言,string 就是個 list of chars 但 Python 是個特例 ---- ### 加法和乘法運算 加法(串接)、乘法(重複接) ```python= name = "Madoka" print("Hello, " + name) # Hello, Madoka print(name * 3) # MadokaMadokaMadoka ``` 回憶一下 `list` 的對應操作 ```python= print(['c'] + [8, 7] + [6, 3]) # ['c', 8, 7, 6, 3] print([8, 7] * 3) # [8, 7, 8, 7, 8, 7] ``` ---- ### 比較運算 兩字串是否相等 ```python= print("abc" == "abc") # True print("abc" != "abc") # False ``` 比較兩字串字典序 ```python= print("abc" < "abd") # True print("abc" < "abcd") # True print("abc" > "def") # False ``` [字典序](https://en.wikipedia.org/wiki/Alphabetical_order#Ordering_in_the_Latin_script) 簡單來說就是從頭比到尾、都一樣比長度 ---- ### `in`, `not in` ```python= text = 'abc' print('a' in text) # True print('ab' in text) # True print('ac' in text) # False print('ac' not in text) # True ``` 回憶一下 `list` 的對應操作 ```python= list1 = ['a', 'b', 'c'] print('a' in list1) # True print('ab' in list1) # False <---- 注意這個 ``` 問題:怎麼比較 `'a', 'b'` 連續地出現在 list 中? ---- ### `len` ```python= greeting = "Hello" print(greeting) print(len(greeting)) # 5 ``` ---- ### 取第 i 個 ```python= greeting = "Hello" print(greeting[0]) # H print(greeting[1]) # e print(greeting[3]) # l print(greeting[-1]) # o print(greeting[5]) # IndexError: string index out of range ``` 這跟 `greeting = ['H', 'e', 'l', 'l', 'o']` 的差別是? ---- ### Slicing ```python= greeting = "Hello" print(greeting[2:4]) # ll print(greeting[:3]) # Hel print(greeting[2:]) # llo print(greeting[::-1]) # olleH print(greeting[:-3:-1]) # ol ``` 這跟 `greeting = ['H', 'e', 'l', 'l', 'o']` 的差別是? ---- ### 可迭代 Iterable 如果你忘記什麼是 iterable,就是一句話:可以 for 它 ```python= greeting = "Hello" for c in greeting: print(c) ``` --- ## 小補充 iterable [還不錯的教學文件](https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Iterables.html) 我們來看一下 definition 和一些花式用法 --- ## 跟 list 不一樣的地方 ---- ### Immutable 不能改值 ```python= text = "abcde" text[2] = 'f' # TypeError: 'str' object does not support item assignment ``` ---- 如果要改掉某個字的話只能很麻煩地這樣做 ```python= text = "abcde" new_text = text[:2] + "f" + text[3:] print(new_text) # abfde ``` ---- 遠超過範圍的補充:[為什麼 string 是 immutable](https://stackoverflow.com/questions/8680080/why-are-python-strings-immutable-best-practices-for-using-them) ---- 沒有 immutability 的話可能會發生一些怪事 ```python= a = [8, 7, 6, 3] b = a a[2] = 5 print(f"{a=}") # a=[8, 7, 5, 3] print(f"{b=}") # b=[8, 7, 5, 3] # Remark: use id print(f"{id(a)=}") print(f"{id(b)=}") ``` 回憶一下為何我們會看到 a, b 會同時被改值 這是因為他們 reference 到同一個物件 ---- [Why is immutability so important (or needed) in JavaScript?](https://stackoverflow.com/questions/34385243/why-is-immutability-so-important-or-needed-in-javascript) 雖然不是 Python 但是核心精神差不多,可以參考看看 ---- 課間練習: 輸入一行字,輸出第 $i$ 行把第 $i$ 個字元取代成 `*` 後印出 Input: ```txt abcdefg ``` Output: ```txt *bcdefg a*cdefg ab*defg abc*efg abcd*fg abcde*g abcdef* ``` ---- 範例解答 ```python= text = input() for i in range(len(text)): print(text[:i] + '*' + text[i+1:]) ``` --- ## String 的各種操作 (method) ---- String 的操作有[這麼多](https://www.w3schools.com/python/python_ref_string.asp),我們只能帶到幾個常用的 ---- 如果你想知道有沒有什麼簡單的方法達成你想要做的事情,可以試著搜尋看看 - Python turn string uppercase (把每個字元轉成大寫) - Python string first capital (把第一個字轉成大寫) - Python string remove blank (把空白拿掉) --- ### replace 就是搜尋與取代 ```python= lyrics = """Never gonna give you up Never gonna let you down Never gonna run around and desert you """ modified_lyrics = lyrics.replace("Never", "Always") print(modified_lyrics) print(lyrics) ``` 注意:原本的字串不會被改動 ---- 練習一下 1. 把前面範例中的 "gonna" 取代成 "wanna" 2. 把前面範例中的 "gonna" 取代成 "wanna",並且把 "you" 取代成 "me" ---- 範例解答 ```python= new_lyrics = lyrics.replace("gonna", "wanna") ``` ```python= new_lyrics = lyrics.replace("gonna", "wanna").replace("you", "me") ``` --- ### isdigit & isalpha & isalnum 判斷字串中是否全為數字/字母/數字與字母 ---- ```python= print("119".isdigit()) # True print("Hello".isalpha()) # True print("Hello, it's me".isalpha()) # False print("C8763".isalnum()) # True ``` 當然還有很多其他的 is 家族,可以慢慢查 ---- 當然,我們可以不用 `isdigit` 暴力做到這些事情 ```python= s = "271828" flag = True for ch in s: if not "0" <= ch <= "9": # if ch not in "0123456789": flag = False print(flag) ``` 但這樣很累欸 ---- 練習時間 搭配 `isdigit` 和 `len` 判斷一串數字是否為合法的手機號碼 1. 十個數字 2. 前兩位是 09 ```python= text = input() # 請開始你的練習 ``` ---- 範例解答 ```python= text = input() print(len(text) == 10 and text[:2] == "09" and text.isdigit()) ``` --- ### strip ```python= new_text = " aaaaa ".strip() print(f"{new_text=}") # new_text='aaaaa' ``` 把字串頭尾的空白拿掉,並且回傳新的字串 --- ### lower & upper 將字串中的英文字母全變成小寫或大寫 ```python= print("Ko no Dio da!".upper()) # KO NO DIO DA! print("Ko no Dio da!".lower()) # ko no dio da! ``` ---- 一些使用情境:不分大小寫搜尋某個字串,例如說 ```python= want_to_search = "Iso" text = "Isometry, isomorphism, isolated" text.lower().find(want_to_search.lower()) # 下一小節是 find ``` 這可以搭配後面的其他東西做練習 --- ### find 尋找 ```python= lyrics = """Never gonna give you up""" print(lyrics.find("gonna")) # 6 ``` ---- 用法 ```python= string.find(value, start, end) ``` * value: 欲尋找的東東 * start: 從哪裡開始找,預設是 0 * end: 找到哪裡結束,預設是到字串結尾 ---- 把所有 gonna 找到 ```python= lyrics = """Never gonna give you up Never gonna let you down Never gonna run around and desert you """ positions = [] start = 0 value = "gonna" while True: ret = lyrics.find(value, start) if ret == -1: break positions.append(ret) start = ret + len(value) print(positions) ``` --- ### split 把字串切成一堆字串用 list 包起來 ```python= text = "Never gonna give you up" splitted = text.split() print(splitted) # ['Never', 'gonna', 'give', 'you', 'up'] ``` ---- 範例:計算你輸入了幾個英文單字 ```python= words = input().split() print(len(words)) ``` ---- 範例:計算一堆數字的平均 ```python= numbers = list(map(float, input().split())) print(sum(numbers) / len(numbers)) ``` ---- 更詳細的用法 ```python= string.split(separator, maxsplit) ``` `separator`: 用什麼來切,預設是空白 `maxsplit`: 最多切幾個 ---- ```python= a = "1 2 3 4 5".split() b = "1 2 3 4 5".split(' ') c = "1 2 3 4 5".split('3') print(f"{a=}\n{b=}\n{c=}") ``` `split()` 和 `split(' ')` 其實有一些微妙的差異,細節需要注意一下 ---- 這樣會噴錯,為什麼? ```python d = list(map(int, "1 2 3 4 5".split(' '))) ``` ```python ValueError Traceback (most recent call last) /tmp/ipykernel_409726/410874544.py in <module> ----> 1 d = list(map(int, "1 2 3 4 5".split(' '))) ValueError: invalid literal for int() with base 10: '' ``` 因為空字串不能被轉成 `int` ---- 下面這個就不會出事 ```python d = list(map(int, "1 2 3 4 5".split())) ``` --- ### join 把一堆用 list 包起來的字串合成一個字串 ```python= words = ['Never', 'gonna', 'give', 'you', 'up'] print(' '.join(words)) # Never gonna give you up ``` 就是把 split 反過來 ---- 用法 ```python string.join(iterable) ``` 用 `string` 把 `iterable` 裡面的東西全部接起來 `iterable` 裡面必須是一些字串 ---- ```python= ''.join(['1', '2', '3']) # 123 ''.join(map(str, range(10))) # 123456789 ', '.join(map(str, range(10))) # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ''.join([1, 2, 3]) # 會噴錯,為什麼? ``` --- ## 綜合練習 ---- 計算一個英文句子中有幾個字 例如 "Hello, it's me." 有三個, "Never gonna give you up" 有五個字 ---- 判斷一個字是否是回文 例如 "ada", "racecar" 是,"sprout" 不是 ---- 你有另一個不打標點符號的檳友 總是用一些空白代替逗號,用 XD 代替句號 請你用 Python 幫他改成正確的標點符號 ```python= s = '我..熬夜...做..簡報.. 累死..了...XD' \ '熬夜..真的..會...變智障 我該..去睡覺XD' # Ans: '我熬夜做簡報,累死了。熬夜真的會變智障,我該去睡覺。' ``` 感謝竹區講師 [Sean64](https://hackmd.io/@Sean64/py-string#/5) 想出的優質題目 --- ## 作業 [3109](https://neoj.sprout.tw/problem/3109/) [3046](https://neoj.sprout.tw/problem/3046/) --- End

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully