python 資料類型 ---字串

來源:互聯網
上載者:User
字串去除空白 ,strip() , 包括空格,tab鍵, 分行符號

>>> name = "  Frank  ">>> name.strip()'Frank'

字串的分割, split("分隔字元"),分組後成為一個列表

>>> name = "Apple, banbana, orice">>> name.split(",")['Apple', ' banbana', ' orice']>>>

字串的合并 join("串連符")

>>> name = ['Frank', 'Marlon', 'Lee']>>> "|".join(name)'Frank|Marlon|Lee'

判斷空格是否子字串中 “in”

>>> name = "Frank Bain">>> '' in nameTrue>>> "L" in nameFalse>>>

兩種字串格式化列印形式 format

# 第一種表示方法>>> msg = "Hello {name}, your age is {age}">>> msg.format(name="Frank", age=24)'Hello Frank, your age is 24'>>># 第二種表示方法 ,注意index 從0 開始>>> msg = "Hello{0}, your age is {1}, sex is {2}">>> msg.format("Frank", 24, "man")'HelloFrank, your age is 24, sex is man'>>>

字串的判斷

>>> name = "Frank">>> name.upper()'FRANK'>>> name.lower()'frank'>>> name.startswith("F")True>>> name.endswith("k")True>>> name.endswith("a")False
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.