Introduction to five methods to connect using Python strings

Source: Internet
Author: User
I have summarized five methods for connecting Python strings: The first method for adding a plus sign (1) and those with programming experience. it is estimated that many languages use a plus sign to connect two strings, python also summarizes five methods for Python string connection:

1. plus sign

First, people with programming experience may know that many languages use the plus sign to connect two strings. in Python, the plus sign is also used to directly connect two strings;

print 'Python' + 'Tab'

Result:

PythonTab
2. comma

The second type is special. use commas to connect two strings. if the two strings are separated by commas, the two strings will be connected, but there will be an extra space between the strings;

print 'Python','Tab'

Result:

Python Tab
3. direct connection

The third type is exclusive to ython. as long as the two strings are put together and there is blank or no blank in the middle, the two strings will be automatically connected to a string;

print 'Python''Tab'

Result:

PythonTab
print 'Python'   'Tab'

Result:

PythonTab
4. Format

The fourth feature is quite powerful. I have used the printf function in C language for reference. if you have the C language basics, read the documentation. In this way, use the symbol "%" to connect a string and a group of variables. the special mark in the string will be automatically replaced with the variable in the variable group on the right:

print '%s %s'%('Python', 'Tab')

Result:

Python Tab
Fifth join

It is a skill to use the string function join. This function accepts a list and uses strings to connect each element in the list in sequence:

str_list = ['Python', 'Tab']a = ''print a.join(str_list)

Result:

PythonTab

The above is a detailed description of the five methods to connect using Python strings. For more information, see other related articles in the first PHP community!

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.