Home >Backend Development >Python Tutorial >5 ways to connect strings in python

5 ways to connect strings in python

尚
forward
2020-06-19 17:18:447251browse

String is a commonly used data type in Python. During the development process, character creation can be intercepted and connected with other character creations. This article introduces 5 ways to connect strings.

5 ways to connect strings in python

1. Plus sign

If you have experience in developing other programming languages, you must know that in many languages, the plus sign is used to connect two strings. In the Python programming language, you can also directly use " " to connect two strings;

 print 'Python' + 'Tab'

Result:

PythonTab

2, comma

Use commas to connect two strings. If the two strings are separated by "comma", then 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

This method is unique to Python. Just put the two Put the strings together, with or without blanks in the middle, the two strings will be automatically concatenated into one string;

print 'Python''Tab'

Result:

PythonTab

4. Formatting

This method is relatively powerful and draws on the function of the printf function in C language. If you have a foundation in C language, just read the documentation. This method uses the symbol "%" to connect a string and a group of variables. The special marks in the string will be automatically replaced with the variables in the variable group on the right:

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

Result:

Python Tab

5. The join() function

is a skill, using the string function join. This function accepts a list and then concatenates each element in the list with a string:

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

Result:

PythonTab

More related knowledge Please pay attention to python video tutorial column

The above is the detailed content of 5 ways to connect strings in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete