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

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss...