How to concatenate python strings

爱喝马黛茶的安东尼
Release: 2019-06-17 16:42:46
Original
4279 people have browsed it

Summarized the 5 methods of Python string connection:

Related recommendations: "python video"

How to concatenate python strings

plus sign
The first one, people with programming experience probably know that in many languages, the plus sign is used to connect two strings. The same is true in Python. You can directly use " " to connect two strings. ;

print 'Python' + ‘Tab’
Copy after login

Result:

PythonTab
Copy after login
Copy after login
Copy after login
Copy after login

Comma
The second one is more special, use comma to connect two strings, if the two strings use "comma" separated, then the two strings will be concatenated, but there will be an extra space between the strings;

print 'Python','Tab’
Copy after login

Result:

Python Tab
Copy after login
Copy after login

Direct connection

The third type is also unique to Python. As long as two strings are put together, with or without blanks in the middle, the two strings will automatically be concatenated into one string;

print 'Python''Tab’
Copy after login

Result:

PythonTab
Copy after login
Copy after login
Copy after login
Copy after login
print 'Python'   'Tab’
Copy after login

Result:

PythonTab
Copy after login
Copy after login
Copy after login
Copy after login

Formatting

The fourth function is more powerful and draws on C language If you have a C language foundation, you will know the functions of the printf function in 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
Copy after login
Copy after login

join

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

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

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!