Python 進階變數類型 --- 字串

來源:互聯網
上載者:User

標籤: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 進階變數類型 --- 字串

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.