Python-multiple methods of string connection and python-multiple methods

Source: Internet
Author: User

Python-multiple methods of string connection and python-multiple methods

There are many string Connection Methods in python. I am writing code today. By the way, I would like to sum up the following:

The most primitive string connection method: str1 + str2
New python string connection Syntax: str1, str2
Strange string method: str1 str2
% Connection string: 'name: % s; sex: % s' % ('Tom ', 'male ')
String list connection: str. join (some_list)

First, as long as you have programming experience, we all know that you can directly use "+" to connect two strings:

>>> Print ('Jim '+ 'green ')
Jimgreen

The second type is special. If the two strings are separated by commas, the two strings will be connected. However, there will be an extra space between the strings:

>>> Print ('Jim ', 'green ')
Jim greem

The third type is exclusive to python. As long as the two strings are put together, there is blank or no blank in the middle: The two strings are automatically connected to one string:

>>> Print ('Jim ''green ')
Jimgreen

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' % ('Jim', 'green '))
Jim, green

The fifth type is a skill. Use the string function join. This function accepts a list and uses strings to connect each element in the list in sequence:

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

In fact, there is also a string connection method in python, but not many are used, that is, string multiplication, such:

In fact, there is also a string connection method in python, but not many are used, that is, string multiplication, such:

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

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.