String Operations
What do you think will be the output if we run the code below?
Isn't it interesting? Did you think it would be possible to combine two strings together?
This is possible in Python, and if we concatenate two strings, it will join them together!
Look at the code below:
As you can see, the output of these two lines is different. The reason is that the string concatenation operation joins them exactly without any space. Also, each print statement is responsible for printing on one line. Here, since we had two print statements, we got two lines of output.
To make the output of the second line similar to the output of the first line, what modifications should we make? (Multiple options can be selected)
1print("Welcome to Python!") 2print("Welcome to" + "Python!")
If we run this code, we will see that there is no space between the first and the second phrase. So we need to create a space between the two phrases in the strings. This can be done in three ways:
- Add a space at the end of the first phrase, like in the first option.
- Add a space at the beginning of the second phrase, like in the second option.
- Insert a new string that contains a space between the two previous phrases and concatenate them, like in the fourth option.
As you can see, even a single space can be a string. In fact, everything that is on the keyboard of your phone or computer is a character that can be included in a string.