2017-04-26

Java 腦袋學 Python 字串

單雙引號都可以,可以視內容有無單雙引號做因應。

Escape Char

  • \'
  • \"
  • \t
  • \n
  • \\

Raw 字串

可以在單雙引號前加上 r 標示為 Raw 字串,Raw 字串特色是保留特殊字元的 Escape Char
print('I\'m ready') # I'm ready
print(r'I\'m ready') # I\'m ready
Raw 字串主要用在 Regular Expression,因為 re 字串裡需要用到很多反斜線,但反斜線又是 Escape char,導致要用兩個反斜線來表示一個反斜線,讓原本就已經很複雜的 re 字串變得更難以閱讀,Raw 字串讓 re 字串回歸到原本的模樣。

多行字串

可以用三個單引號或雙引號組成多行字串,特色是特殊字元不需要 Escape char,而且可以多行輸入,像 HTML 的 pre 那樣保留原始格式。

s = '''
I'm ready!
    Indentation is OK!
    Space is OK, too!
just like <pre> HTML tag
'''
print(s)

多行註解

之前學過用 # 表示單行註解,這裡可以借用多行字串的語法(拿掉指定語法)延伸出多行註解,單雙引號都可以。
# Browse the docs online or download a copy of your own.
# Python's documentation, tutorials, and guides are constantly evolving.
# Get started here, or scroll down for document

"""
Browse the docs online or download a copy of your own.
Python's documentation, tutorials, and guides are constantly evolving.
Get started here, or scroll down for document
"""
'''
Browse the docs online or download a copy of your own.
Python's documentation, tutorials, and guides are constantly evolving.
Get started here, or scroll down for document
'''

list-like

Python str 是一種 list-like  的資料類型,可以看作是由單一字元組成的 list,可以利用 list() 與 tuple() 測試 list-like。
str = 'Python'
print(str) # Python
print(list(str)) # ['P', 'y', 't', 'h', 'o', 'n']
print(tuple(str)) # ('P', 'y', 't', 'h', 'o', 'n')
許多對 list 讀取的操作都可以用在 str 上,但由於 list 是 mutable,而 str 是 immutable,所有涉及改值的 list 操作,都會得到 TypeError。
str = 'Python'
print(type(str)) # <class 'str'>
print(str.index('t')) # 2
print(str[2:4]) # th
print('y' in str) # True
print('T' not in str) # True
print(len(str)) # 6
for c in str:
    print(c)
# P
# y
# t
# h
# o
# n
str[1] = 'Y' # TypeError
要改變 str 內容,只能新建一個 str。
str = str[0] + str[1].upper() + str[2:]
print(str); # PYthon

字串的 method

所有的字串 method 都不會修改原字串,而是回傳新字串,因為字串是 immutable。
# upper() & lower()
print ('Python'.upper() == 'PYTHON') # True
print ('Python'.lower() == 'python') # True

# isupper()
print('UPPER'.isupper()) # True,所有字母必須都是大寫
print('Upper'.isupper()) # False
print('upper'.isupper()) # False
print('128'.isupper()) # False,至少要有一個字母
print('U128'.isupper()) # True,所有字母必須都是大寫

# islower()
print('UPPER'.islower()) # False,所有字母必須都是小寫
print('Upper'.islower()) # False
print('upper'.islower()) # True
print('128'.islower()) # False,至少要有一個字母
print('u128'.islower()) # True,所有字母必須都是小寫

# isalpha()
print('Alpha'.isalpha()) # True,只能有字母且非空字串
print('Alpha9'.isalpha()) # False
print('999'.isalpha()) # False
print(''.isalpha()) # False

# isdecimal()
print('Alpha'.isdecimal()) # False,只能有數字且非空字串
print('Alpha9'.isdecimal()) # False
print('999'.isdecimal()) # True
print(''.isdecimal()) # False

# isalnum()
print('Alpha9'.isalnum()) # True,只能有字母與數字且非空字串
print('Alpha'.isalnum()) # True,只有字母是 ok 的
print('999'.isalnum()) # True,只有數字也是 ok 的
print(''.isalnum()) # False

# isspace()
print(' \n\t'.isspace()) # True,只能有空白、\n 與 \t,且非空字串
print(''.isspace()) # False

# istitle()
print('Beginner Guide To Python'.istitle()) 
# True,所有單字都是大寫字母開頭,其餘都是小寫字母且非空字串
print('Python goes!'.istitle()) # False
print(''.istitle()) # False
print('20 Books'.istitle()) # True,不是單字不列入考慮

# startswith() & endswith()
print ('Python'.startswith('Py')) # True,區分大小寫
print ('Python'.endswith('ON')) # False

# join() & split()
print (', '.join([ 'Apple', 'Banana', 'Chocolate'])) 
# Apple, Banana, Chocolate,是用 separate char 去做 join()
print ('Apple, Banana, Chocolate'.split(', ')) 
# ['Apple', 'Banana', 'Chocolate'],separate char 預設為空白

# rjust(), ljust() & center()
print('Python'.rjust(10, '-')) # ----Python,r 是指字靠右
print('Python'.ljust(10, '-')) # Python----,l 是指字靠左
print('Python'.center(10, '-')) # --Python--

# rstrip(), lstrip() & strip()
print('   Python   '.rstrip()) #    Python,刪除所有的空白、\n、\t字元
print('   Python   '.lstrip()) # Python   
print('   Python   '.strip()) # Python
print('=+=Python+=+'.rstrip('+=')) # =+=Python,刪除所有指定的字元
print('=+=Python+=+'.lstrip('+=')) # Python+=+
print('=+=Python+=+'.strip('+=')) # Python

# find() 尋找第一個符合字串的 index
print('Return the lowest index in the string'.find('t')) # 2, Re[t]urn
print('Return the lowest index in the string'.find('t', 5)) # 7, [t]he
print('Return the lowest index in the string'.find('t', 3, 7)) # -1

# replace()
print('Return a copy of the sequence with all occurrences of subsequence old replaced by new'.replace('o', '喔')) 
# Return a c喔py 喔f the sequence with all 喔ccurrences 喔f subsequence 喔ld replaced by new
print('Return a copy of the sequence with all occurrences of subsequence old replaced by new'.replace('o', '喔', 3)) 
# Return a c喔py 喔f the sequence with all 喔ccurrences of subsequence old replaced by new

# count() 統計指定字串的出現次數
print('Return the number of non-overlapping occurrences of subsequence'.count('t')) # 2

# zfill() 最常用的補零,但是只能補零唷~
print(str(19).zfill(5)) # 00019

# capitalize() 真的有人需要這個?
print("the first byte capitalized and the rest lowercased".capitalize()) 
# The first byte capitalized and the rest lowercased

char-int 轉換

# ord() & chr(),這是內建的函式,不是字串的 method
print(ord('a')) # 97
print(chr(97)) # a
官方文件 在這

縮排 & 續行符號

Python 幾個不用管縮排的地方,list item 是其中一個,因為有時候 item 太多時,勢必要換行。
list = ['A', 'B',
        'C',
'D',
    'E',
        'F']
print(list); # ['A', 'B', 'C', 'D', 'E', 'F']
另外 tuple 與 dict 也都是。

比較特別的是搭配續行符號 \ 的長字串。
str = 'Whether you are new to programming or an experienced developer, ' + \
'it is easy to learn and use Python. ' + \
    'Python source code and installers are available ' + \
        'for download for all versions! '
而且用了續行符號就一定要換行,不然也會報錯。
str = 'Whether you are ' + 'new to programming ...' # OK
str = 'Whether you are ' + \
      'new to programming ...' # OK
str = 'Whether you are ' + \ 'new to programming ...' # Error - unexpected character...
str = 'Whether you are ' +
'new to programming ...' # Error - invalid syntax
順便提一個續行符號的其他用法,條件語句。
str = 'Python'
if 'P' in str and 'y' in str \
and 't' in str \
    and 'h' in str\
        and 'o' in str \
            and 'n' in str:
    print('got u') # got u
目前還不確定,是不是所有語句都可以使用續行符號?

官方文件
---
---
---

沒有留言:

張貼留言