6 ways to stitch a python string

Source: Internet
Author: User
Tags python string concatenation

Add by ZHJ: For a multi-line string connection, the 6th connection method is convenient and no additional space is added when connecting.

Original: http://www.cnblogs.com/bigtreei/p/7892113.html

1. Plus

First, people with programming experience know that many languages use a plus sign to connect two strings, and Python also uses "+" directly to connect two strings;

print ' Python ' + ' Tab '

Results:

Pythontab
Back to top of 2. Comma

The second is special, use a comma to concatenate two strings, if the two strings separated by a "comma", then the two strings will be concatenated, but there will be a space between the strings;

print ' Python ', ' Tab '

Results:

Python Tab
Back to top of 3. Direct connection

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

Example 1:

print ' Python ' Tab '

Results:

Pythontab

Example 2:

print ' Python '   Tab '

Results:

Pythontab
Back to top of 4. Formatting

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 '% (' Python ', ' Tab ')

Results:

Python Tab
Back to top of 5. Join

Use a string function join; This function receives a list and then connects each element of the list with a string:

Str_list = [' Python ', ' Tab ']a = ' Print A.join (str_list)

Results:

Pythontab
Back to top of 6. Multi-line string concatenation ()
s = (' select * ' from     atable '     where id=888 ') print S, type (s) #输出select *from atablewhere id=888 <type ' str ' >

Python encounters unclosed parentheses, automatically stitching multiple lines into one line, and does not treat line breaks, leading spaces as characters, compared to three quotes and line breaks.

Python string concatenation of 6 ways (GO)

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