How python outputs the strings you need and how to connect them

迷茫
Release: 2017-03-25 13:01:04
Original
2600 people have browsed it

The most originalStringConnection method: str1 + str2

python New string concatenation syntax: str1, str2
Strange string method: str1 str2
% Concatenation string: 'name:%s; sex:%s ' % ('tom', 'male')
String list connection: str.join(some_list)

The first method, as long as anyone with programming experience, probably knows it, directly use "+" to connect two strings:

>>> print('jim'+'green')
jimgreen

The second type is special, if two strings use "comma" separated, then the two strings will be concatenated, but there will be an extra space between the strings:

>>> print('jim',' green')
jim greem

The third type is also unique to python. Just put two strings together, with or without blanks in between: two strings Automatically connected to a string:

>>> print('jim''green')
jimgreen

The fourth function is more 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'%('jim','green'))
jim,green

The fifth type is a skill, use String function join. This function accepts a list and then concatenates each element in the list with a string:

var_list = ['tom', 'david', 'john']
a = '
'
>>> print(a.join(var_list) )
tom

david

john

In fact, python There is also a string connection method in , but it is not used much, which is string multiplication, such as:
a = 'abc'
>>> print(a*3)

abcabcabc#########

The above is the detailed content of How python outputs the strings you need and how to connect them. For more information, please follow other related articles on the PHP Chinese website!

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!