python列表以空格分割_在空格上创建Python列表拆分

本文详细介绍了Python中字符串的split方法,包括其默认和指定分隔符的使用,以及maxsplit参数的影响。通过实例展示了如何利用split方法拆分姓名,并将其赋值给单独的变量。split方法在处理文本数据时非常实用,能有效提取信息。
摘要由CSDN通过智能技术生成

字符串.拆分(s[,sep[,maxsplit]])Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument sep is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. If maxsplit is given, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (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).full_name = "My Name".split()

它将返回一个列表:

^{pr2}$

要将名字和姓氏分配给可以解压缩的变量:In [3]: full_name = "My Name"

In [4]: first, last = full_name.split()

In [5]: first

Out[5]: 'My'

In [6]: last

Out[6]: 'Name'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值