6 ways to stitch a python string

Source: Internet
Author: User
Tags python string concatenation

Python string concatenation of 6 ways:

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;

1 print‘Python‘ +‘Tab‘

Results:

1 PythonTab
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;

1 print‘Python‘,‘Tab‘

Results:

1 Python Tab

3. Direct connection

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

1 print‘Python‘‘Tab‘

Results:

1 PythonTab
1 print‘Python‘‘Tab‘

Results:

1 PythonTab

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:

1 print‘%s %s‘%(‘Python‘‘Tab‘)

Results:

1 Python Tab

The fifth kind of join

It's a technique, a function join using a string. This function takes a list and then connects each element of the list with a string:

123 str_list =[‘Python‘‘Tab‘]= ‘‘printa.join(str_list)

Results:

1 PythonTab

Sixth multi-line string concatenation ()
s = ('select *'from     atable'     where id= 888')print  s, type (s)# output select * from  '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.

6 ways to stitch a python string

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.