Python字符串| join()方法与示例

join() is an in-built method in Python and it is used to join elements of the list, string etc with the given str separator.

join()是Python中的一种内置方法,用于通过给定的str分隔符连接列表,字符串等元素。

Note: Method is called with the separator and as arguments elements are provided, then the final (returned) string is a joined string by the separator string.

注意:方法是使用分隔符调用的,并提供了arguments元素,然后最后一个(返回的)字符串是分隔符字符串连接的字符串。

Syntax:

句法:

 String.join(iterable)

Example 1: Joining string with hyphen ('-') character

示例1:使用连字符('-')连接字符串

# s as separator string 
s = "-"
# str as sequence of strings
str = ("Hello", "World", "How are?")

# join and print the string
print (s.join(str))

Output

输出量

    Hello-World-How are?

Example 2: Joining string with spaces, a student detail is provided as string sequences

示例2:将字符串与空格连接,将学生详细信息作为字符串序列提供

# s as separator string (contains - space)
s = " "

# a student details
first_name = "Amit"
second_name = "Shukla"
age = "21"
branch = "B.Tech"

# creating string sequences
str = (first_name, second_name, age, branch)

# value of str before joining
print "str before join..."
print (str)


# join and print the string
print "str after join..."
print (s.join(str))

Output

输出量

    str before join...
    ('Amit', 'Shukla', '21', 'B.Tech')
    str after join...
    Amit Shukla 21 B.Tech


翻译自: https://www.includehelp.com/python/string-join-method-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值