In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to concatenate strings in python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Plus sign connection
First, in the form of the + sign:
> a, b = 'hello',' world'
> a + b
'hello world' comma connection
Second, through, in the form of commas:
> a, b = 'hello',' world'
> print (a, b)
Hello world
However, one thing to note when using comma form is that it can only be used for print printing, and the assignment operation generates tuples:
> a, b
('hello',' world')
Python's meditation note: in fact, this is not a way of concatenating strings, because 'hello',' world' exists as a tuple, assigning values to variables an and b by unpacking.
Direct connection
Third, you can connect directly with or without spaces:
Print ('hello'' world')
Print ('hello''world')
Python's Zen Note: this is a kind of grammatical candy in Python. Consecutive strings are automatically concatenated into a string. There are no two string objects in memory. (code can slide left and right)
> def x ():
... A = 'aura'
...
> > dis.dis (x)
2 0 LOAD_CONST 1 ('ab')
3 STORE_FAST 0 (a)
6 LOAD_CONST 0 (None)
9 RETURN_ value%
Fourth, use the% operator.
Prior to Python 2.6, the% operator was the only way to format strings, and it could also be used to concatenate strings.
Print ('% s% s'% ('hello',' world')) format function
Fifth, use the format method.
The format method is a string formatting method that appears in Python 2.6 instead of the% operator, and can also be used to concatenate strings.
Print ('{} {} '.format (' hello', 'world') join function
Sixth, use the join built-in method.
A string has a built-in method, join, whose argument is a sequence type, such as an array or tuple.
Print ('- '.join ([' aa', 'bb',' cc'])) f-string
Seventh, use f-string mode.
Python 3.6introduces Formatted String Literals (literal formatted string), which is an evolutionary version of the% operator and the format method for short. The method of concatenating strings using f-string is similar to using the% operator and the format method.
> aa, bb = 'hello',' world'
> f' {aa} {bb}'
'hello world' asterisk *
Eighth, use the * operator.
> aa = 'hello'
> aa * 3
'hello hello hello'
Python's meditation note: the * operator is actually an operator overloading operation, and the corresponding magic method is _ _ mul__
> a = [1]
> axi2
[1, 1]
> a.cake mulberry _ 3
[1,1,1] Summary
The + operator is recommended when concatenating a small number of strings.
If there are high performance requirements and the python version is above 3.6, it is recommended to use f-string. For example, f-string is much better readable than the + sign in the following cases:
A = f 'name: {name} Age: {age} gender: {gender}'
B = 'name:' + name + 'Age:' + age + 'gender:' + gender is here about how to concatenate strings in python. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.