为了账号安全,请及时绑定邮箱和手机立即绑定

python中的拆分函数以将字符串与逗号分开

python中的拆分函数以将字符串与逗号分开

慕婉清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贡献1827条经验 获得超9个赞

str.split()救援:


a='g5s,12-04-2019'

b, c = a.split(",")


查看完整回答
反对 回复 2022-06-02
?
陪伴而非守候

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={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.


查看完整回答
反对 回复 2022-06-02
  • 2 回答
  • 0 关注
  • 138 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号