Skip to main content

How to use strings in Python

How to concatenate strings in Python

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:

firstName = "Adam"

surname = " Sinicki"

name = firstName + 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:

firstName = "Adam"

surname = " Sinicki"

name = firstName + 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.



source https://www.androidauthority.com/how-to-concatenate-a-string-in-python-1135682/

Comments

Popular posts from this blog

How to unhide or show folders in mx player list

In this blog post, I tell you about how to Show or Hide folders from MX player list. There are two methods to Show folder from MX Player list. Method 1: Unhide / show folders If you want to temporarily Show / Unhide hidden folder from MX Player list, then go to Settings and untick Recognize .nomedia ".  Method 2: Permanently unhide / show folder: Open memory by any file explorer and I recommend X-Plore, and open the folder that is Hidden and find the file " .nomedia ". If you didn't find it, you should first enable "Show files hidden files that starts with .(dot)". Delete the file and you just need to refresh MX Player list to take changes. Note: MX Player always hide those folders which file " .nomedia " exists.

How to use a web API from your Android app

Credit: Adam Sinicki / Android Authority A Web API is an online “application programming interface” that allows developers to interact with external services. These are the commands that the developer of the service has determined will be used to access certain features of their program. It is referred to as an interface because a good API should have commands that make it intuitive to interact with. An example of this might be if we want to get information about a user from their social media account. That social media platform would likely have a web API for developers to use in order to request that data. Other commonly used APIs handle things like advertising ( AdMob ), machine learning ( ML Kit ), and cloud storage. It’s easy to see how interacting with these types of services could extend the functionality of an app. In fact, the vast majority of successful apps on the Play Store will use at least one web API! In this post, we’ll explore how to use a web API from withi...

How to Hide and Show Videos in MX Player

We use android apps to hide our personal data from gallery or any viewing source. These Applications use mobile memory and use a lot of RAM and these type of Applications is major cause of battery power wastage. Now I will tell you about a tip  to hide videos from MX Player list without using Android Application. Step 1: Put all those videos which you want to hide in a folder. Step 2: Create an blank file and name it as ".nomedia " and  place this file inside the folder  where are targeted hidden videos are present. Step 3: Now check MX Player and the videos and those videos which you put on that folder would have been ignored (hidden) Refresh MX Player list if these showed again. To show hidden Videos just remove the " .nomedia " file. Note: No need to hide the folder and there is no need of any application.  MX Player hide the videos where the file " .nomedia " exists.