python--multiple ways to concatenate strings

Source: Internet
Author: User

Python has a lot of string connections, today in writing code, by the way, from the most original string connection to string list connection, we feel the following:

The most primitive way to connect strings:str1 + str2
Python new string connection syntax:str1, str2
Strange string way:str1 str2
% connection string:' name:%s; sex:%s '% (' tom ', ' Male ')
String list connection:str.join (some_list)

First, presumably as long as the programmer has the experience of the people, it is estimated that the direct use of "+" to connect two strings:

>>> print (' jim ' + ' green ')

The second is special, if two strings are separated by "comma", then the two strings will be concatenated, but there will be a space between the strings:

>>> print (' Jim ', ' Green ')
Jim Greem

The third is also Python-specific, as long as the two strings together, the middle of a blank or no blank: two strings are automatically connected to a string:

>>> print (' jim ' Green ')

The fourth function is more powerful, drawing on the function of the printf function in C, if you have a C language basis, look at the document to know. In this way, a string and a set of variables are concatenated with the symbol "%", and the special tags in the string are automatically replaced with the variables in the right variable group:

>>> print ('%s,%s '% (' Jim ', ' Green ')

The fifth type is the technique, using the string function join. This function takes a list and then connects each element of the list with a string:

Var_list = [' Tom ', ' David ', ' John ']
A = ' # # # '
>>> Print (A.join (var_list))
tom## #david # # #john

In fact, Python also has a way of string connection, but not much, is the string multiplication, such as:

In fact, Python also has a way of string connection, but not much, is the string multiplication, such as:

A = ' abc '
>>> Print (a*3)

python--multiple ways to concatenate strings

Related Article

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: 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.