Get The Learn to Code Course Bundle!
Enroll in The Complete Python Course on Udemy!
What are some of the fun and useful things that we can do with strings in Python? Go ahead and open up a either your IDE, terminal, or command prompt and follow along. I find that if you’re simply told to do something you may not remember the procedure next time. However, if you actually perform that actions yourself, it is much more likely to stick. So, the first thing we are going to cover is concatenating strings. Concatenating is essentially gluing two things together. Below is an example of concatenating two strings together:
"Hello, " + "Nick"
As you can see, you concatenate two strings by using the “+” operator. You can also use the string function to convert an integer to a string like so:
"This costs" + str(6) + "dollars
You can also perform mathematical operations within the string function parameter.
"This costs" + str(6+5) + "dollars"
So, how do we do the opposite of concatenation? We can split strings by using the “:” operator.
This all may not seem very exciting, and you may or may see how this could be useful, but you will see later on why concatenating is useful when coding.