How to use the Python join character join function

Source: Internet
Author: User

String module, so it requires two parameters, one is a list, and the other is a separator. And when called, as Cnxo shows, if you import a string module:

Import string
You need to use String.Join () for this.

But now the string object itself has such a method that it requires only one delimiter parameter. Usage is:

' + '. Join (List1)


The simplest we use is the Join function, the instance

print '. Join (List (' Hello '))


Connect the data

Word1 = "A"
Word2 = "few"
Word3 = "good"
Word4 = "Words"
Wordlist = ["A", "few", "more", "good", "words"]

Print "Words:" + word1 + word2 + word3 + word4
Print "List: +". Join (Wordlist)

Instance

Word1 = "A"
Word2 = "few"
Word3 = "good"
Word4 = "Words"
Wordlist = ["A", "few", "more", "good", "words"]


#Joining a list of words
Sentence = "Second:"
For word in wordlist:
Sentence + = "" + Word
Sentence = "."
Print sentence

Take a look at the example above

# Splitting strings
string1 = "A, B, C, D, E, F"

Print "String is:", string1
Print "Split string by Spaces:", String1.split ()
Print "Split string by commas:", String1.split (",")
Print "Split string by commas, max 2:", String1.split (",", 2)
Print

# Joining Strings
List1 = ["A", "B", "C", "D", "E", "F"]
string2 = "___"

Print "List is:", List1
print ' Joining with '%s ':%s '
% (string2, String2.join (List1))
print ' Joining with '-.-': ', '-.-'. Join (List1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.