第一期 Python 程式設計入門共學營作業任務五


介紹字串物件─str.split(sep=None, maxsplit=-1)

官方文件的介紹:

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

split(sep=None, maxsplit=-1):sep可以指定分隔符號;maxsplit=-1或預設,依分隔符號對字符串進行所有可能的分割,範例如下:

https://static.coderbridge.com/img/hufeigopython/11f65b0f5d3243119ed0d2cd2785c67e

程式設計實作題

[https://repl.it/@hufeigopython/Di-Yi-Qi-Python-Cheng-Shi-She-Ji-Ru-Men-Gong-Xue-Ying-Zuo-Ye-Ren-Wu-Wu]

#第一期Python程式設計入門共學營






留言討論