Here is the most comprehensive Python string concatenation summary, quickly collection!

Source: Internet
Author: User
Tags python string concatenation

There are many ways to concatenate strings in Python, here is a simple summary, it should be more comprehensive, convenient for later review.

Plus connection

The first, by the form of the + sign:

>>> A, b = ' hello ', ' world '

>>> A + b

' Hello World '

Comma connection

The second, by way of a comma, is:

>>> A, b = ' hello ', ' world '

>>> print (A, B)

Hello World

However, in the form of commas, it is important to note that only print printing can be used, and the assignment operation will narimoto the group:

>>> A, b

(' Hello ', ' world ')

Note: In fact, this is not a way to concatenate strings, because ' hello ', ' world ' will exist as a tuple, assigned to variables A and B by unpacking (unpacking).

Direct connection

Third, there are no spaces in the middle of the direct connection:

Print (' Hello ' world ')

Print (' Hello ' world ')

Python Zen Note: This is a syntactic sugar in python, and successive strings are automatically stitched together into a string. There are no two string objects in memory. (Code can swipe left and right)

>>> Defx ():

... a = ' a ' B '

...

>>> Dis.dis (x)

20load_const1 (' AB ')

3store_fast0 (a)

6load_const0 (None)

9return_value

Percent percent%

The fourth type, use the% operator.

Before Python 2.6, the% operator was the only way to format a string, and it could also be used for a connection string.

Print ('%s '%s '% (' hello ', ' world ')

Format function

Fifth type, use the Format method.

The Format method is a string formatting method that appears in Python 2.6 instead of the% operator, and can also be used to concatenate strings.

Print (' {} {} '. Format (' Hello ', ' world ')

Join function

Sixth, use the join built-in method.

A string has a built-in method join whose argument is a sequence type, such as an array or a tuple.

Print ('-') join ([' AA ', ' BB ', ' cc '))

F-string

The seventh type, using F-string way.

Python 3.6 introduced the formatted string literals (literal formatting string), abbreviated f-string,f-string is the% operator and the evolution version of the format method, using the F-string connection string method and using the% The operator, format method is similar.

>>> aa, bb = ' hello ', ' world '

>>> f ' {AA} {bb} '

' Hello World '

Asterisk

The eighth type, use the * operator.

>>> aa = ' Hello '

>>> AA

' Hello hello hello '

Note: * operator is actually an operator overloaded operation, the corresponding magic method is __mul__

>>> a = [1]

>>> a*2

[About]

>>> a.__mul__ (3)

[1,1,1]

Summary

When connecting a small number of strings, it is recommended to use the + sign operator.

If you have high performance requirements, and the Python version is above 3.6, we recommend using F-string. For example, the following f-string readability is much better than the + sign:

A = F ' name: {name} Ages: {age} Gender: {gender} '

b = ' Name: ' + name + ' Age: ' + ages + ' sex: ' + gender

Joins and F-string are recommended when connecting a large number of strings, and depending on the version of Python you use and the readability requirements you choose.

I have done a test, python3.6, the amount of data is not small case + operation even faster than join operation. You have encountered what God operation, welcome message.

I have a public number, and I often share some of the stuff about Python technology, and if you like my share, you can search for "Python language learning"
Concern

Welcome to join thousands of people to exchange questions and answers skirt: 699+749+852

Here is the most comprehensive Python string concatenation summary, quickly collection!

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.