How to concatenate a string in python and more
Python is one of the easiest programming languages to get to grips with, and also one of the most powerful and in-demand. Learning Python is not only fun then, but also a fantastic career move. One of the first concepts you’ll need to familiarize yourself with to that end is how to use strings. In this post, you’ll learn how to create, change, and concatenate strings in Python.
Also read: How to round in Python
So… what’s a string?
In case you’re scratching your head, a string is a variable that represents a series of alphanumeric characters and symbols. This could be a name, a word, or a whole sentence.
Strings are useful when you want to show text on the screen that might change in response to user input. They’re also useful for storing data: for instance names in a database.
Python makes it extremely easy to create strings. All you need to do is choose the word that is going to represent your string, and then store the data using the equals sign.
Code
Copy Textname = “Adam”
Creates a string called “name” and then stores “Adam” as the value.
Now you can show the value of name on the screen like so:
Code
Copy Textprint(“Hello”, name)
Note that using a comma this way will insert a space between the two elements.
If you write:
Code
Copy Textname = input(“Please enter your name:”) print(“Hello”, name)
Then the user will be able to input their name and then be greeted personally!
Now you know how to create a string, next we need to learn how to change the value, how to get specific characters, and how to concatenate a string in Python.
How to concatenate a string in Python
If you want change the value of a string, you simply reassign it with another “=”.
For example:
Code
Copy Textname = “Adam” name = “Barry” print(name)
Will print “Barry” on the screen.
If you want to know how to concatenate a string in Python – meaning that you are adding to the end or combing two strings – then you simply need to use the plus symbol. For instance:
Code
Copy Textname = "Adam" name = name + " Sinicki" print(name)
The other option for how to concatenate a string in Python is simply adding two together:
Code
Copy Textfirst_name = "Adam" surname = " Sinicki" name = first_name + surname print(name)
Getting length and characters
If you want to get the length of a string, you can do so using len().
Code
Copy Textlen(surname)
This, as you might imagine, will tell you how long the string is.
This can be useful if you ever want to get a specific character from your string:
Code
Copy Textfirst_name = "Adam" surname = " Sinicki" name = first_name + surname print(name[7])
Knowing the length of a string before trying to retrieve a letter is useful, as it ensures we won’t try to get a character that falls outside the length of the string – which would cause an error.
You can return a range of characters from within a string like so:
Code
Copy Textprint(name[3:7:1])
Here, you are asking for the first letter in range : last letter in range : step count.
More tricks
Now you know how to concatenate a string in Python, how to return specific characters and more! Here are just a couple of other neat things you might want to do…
You might find yourself wondering if a certain value is contained within a string. For example, this might mean looking for a keyword within a sentence. You can do this with “in.” This returns a true or false value (Boolean) which can be used for control flow.
Finally, you can also search within a string like so:
Code
Copy Textname.find(“Sinicki”)
If Python finds a match, then it will return the index of that substring. If it doesn’t find it, it will return the value “-1”.
So there you go! Now you know how to concatenate a string in Python and so much more! Let us know what else you want to know down below.
For more developer news, features, and tutorials from Android Authority, don’t miss signing up for the monthly newsletter below!
CommentsncG1vNJzZmivp6x7orrDq6ainJGqwam70aKrsmaTpLpwtM6wZK2nXZi8r6%2FArZynmaSaeqJ50q2poqaXYravec%2Byq6GnnmJ%2Bcn%2BUb29rZw%3D%3D