慕婉清6462132
2022-06-02 16:23:21
如何使用 split'g5s,12-04-2019'與逗號分隔。a='g5s,12-04-2019'如果在拆分之后b = 'g5s',我會搜索結果c='12-04-1=2019'
2 回答

陪伴而非守候
TA貢獻1757條經驗 獲得超8個贊
您應該使用x.split()并傳遞逗號作為輸入參數,就像這樣x.split(",")。這意味著輸入參數是分隔符。
該split方法返回一個單獨包含元素的列表。你可以一一解開。像這樣:b, c = a.split(",")
代碼:
a = "g5s,12-04-2019"
print("a={a}".format(a=a))
b, c = a.split(",")
print("b=, c={c}".format(b=b, c=c))
輸出:
>>> python3 test.py
a=g5s,12-04-2019
b=g5s, c=12-04-2019
供參考:
方法的文檔字符串split。
S.split(sep=None, maxsplit=-1) -> 字符串列表
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
添加回答
舉報
0/150
提交
取消