5 ways to connect strings in python

Release: 2020-06-19 17:18:44
forward
7194 people have browsed it

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'
Copy after login

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'
Copy after login

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'
Copy after login

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')
Copy after login

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)
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template