Chapter 8
Concatenating Text Strings Python
In Chapter 1 you figured out how to show a string on the screen, coding it thusly.
Print ("Hello, World!")
In Chapter 2, you discovered that you could utilize a variable to do something very similar
1 greeting = "Hello,
World!"
2 print (greeting)
However, assume you needed to break the welcome into two sections, and allocate each part to a different variable, similar to this:
1 greeting = "Hello"
2 addressees = "World"
You advise Python to join the two strings thusly:
Whole greeting = welcoming +
addressee
Everything necessary is
an or more sign.
Presently, in the event that you code…
Print (whole greeting)
… Python shows
HelloWorld
That is not exactly
what we need, so how about we add some more connection…
1 greeting = "Hi"
2 separators = ", "
3 addressees = "World"
4 punc = "!"
5 whole greeting = welcoming +
separators + addressee
+ punch
6 print (whole greeting)
Python shows Hello,
World!
In the code above, I
doled out the four pieces of the entire welcome to four unique factors. At that
point, I connected them and relegated the blend to the variable whole greeting.
Python is glad to link
strings just as factors…
The whole greeting = "Hi, "
+ "World!"
…or a blend of factors
and strings…
Whole greeting = "Hi" +
separators + "World" + punc
You don't need to
relegate the consequence of a connection to a variable. This would work:
Print ("Hello, " +
"World!")
So, would this:
Print (greeting + separators +
recipient + punc)
…or this:
Print ("Hello" +
separators + "World" + punc)
You can utilize the in
addition to sign to total numbers, and you can utilize it to connect strings.
However, you can't utilize the, in addition, to sign to join strings and numbers.
On the off chance that you compose this code, you get a blunder message:
Print ("The amount of 2 in
addition to 2 is " + 4)
Notwithstanding, on the off chance that you
make that number a string, it'll work…
Print ("The volume of 2 in
adding to 2 is " + "4")
Python shows the amount of 2 in addition to 2 is 4.
No comments:
Post a Comment