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.
So:
name = “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:
print(“Hello”, name)
Note that using a comma this way will insert a space between the two elements.
If you write:
name = 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:
name = “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:
name = "Adam" name = name + " Sinicki" print(name)
This adds my surname to the string. Notice that I also remembered to include a space between the two names!
Also read: How to call a function in Python
The other option for how to concatenate a string in Python is simply adding two together:
first_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().
len(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:
first_name = "Adam" surname = " Sinicki" name = first_name + surname print(name[7])
This returns the character with the index “7”. Note that this is not the 7th character, but actually the 8th, seeing as the first character always has the index “0.” This is the same when using lists in Python.
Also read: How to install Python and start coding on Windows, Linux, or Mac
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:
print(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:
name.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!
0 Commentaires