Computer Programming Coding
Computer Programming Coding


Computer programming coding

Python is one of the easiest programming languages ​​to use and one of the most powerful and sought-after. Learning Python is not only fun, it is also a fantastic step in your career. One of the first concepts that you need to become familiar with for this purpose is the use of strings. This post will show you how to create, modify, and concatenate strings in Python.

Also read: How to round in Python

So … what is a string?

If you’re scratching your head, a string is a variable that represents a series of alphanumeric characters and symbols. This can be a name, a word or a whole sentence.

Strings are useful when you want to display text on the screen that can change based on user input. They are also useful for storing data: for example names in a database.

Python makes it extremely easy to create strings. All you have to do is select the word you want to represent your string and then save the data with the equal sign.

So:

code

name = “Adam”

Creates a string named “Name” and then stores “Adam” as the value.

Now you can see the value of the name on the screen like this:

code

print(“Hello”, name)

Note that using a comma this way inserts a space between the two items.

If you write:

code

name = input(“Please enter your name:”)

print(“Hello”, name)

Then the user can enter their name and will be greeted personally!

Now you know how to create a string. Next, you need to learn how to change the value, get specific characters, and concatenate a string in Python.

How to concatenate a string in Python

If you want to change the value of a string, simply reassign it with a different “=”.

For example:

code

name = “Adam”

name = “Barry”

print(name)

Prints “Barry” on the screen.

In Python, if you want to know how to concatenate a string, i.e. add it to the end or combed two strings, all you have to do is use the plus sign. For example:

code

name = "Adam"

name = name + " Sinicki"

print(name)

This will add my last name to the string. Note that I also remembered to put a space between the two names!

Also read: How to call a function in Python

The other option for concatenating a string in Python is to simply add two together:

code

first_name = "Adam"

surname = " Sinicki"

name = first_name + surname

print(name)

Get length and characters

If you want to get the length of a string you can use this with len ().

code

len(surname)

This shows you how to imagine how long the string is.

This can be useful if you ever want to get a specific character from your string:

code

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 8thth, because the first character always has the index “0”. This also applies to the use of lists in Python.

Also read: Here’s how to install Python and start coding on Windows, Linux, or Mac

Knowing the length of a string before attempting to get a letter is useful as this will ensure that we are not trying to get a character that is outside the length of the string – which would result in an error.

You can return a range of characters from a string like this:

code

print(name[3:7:1])

Here you ask for the first letter in the area: the last letter in the area: number of steps.

More tricks

Now that you know how to concatenate a string in Python, return certain characters, and more! Here are just a few other nice things you might want to do …

You may be wondering if a particular value is in a string. This can mean, for example, that you are looking for a keyword within a sentence. You can do this with “in”. This returns a true or false value (Boolean) that can be used for the control flow.

Finally, you can also search within a string like this:

code

name.find(“Sinicki”)

When Python finds a match, it returns the index of that substring. If it doesn’t find it, it returns “-1”.

Here you go! Now that you know how to concatenate a string in Python, and more! Let us know what else you want to know below.


For more developer news, features, and tutorials from Android Authority, don’t sign up for the monthly newsletter below!

LEAVE A REPLY

Please enter your comment!
Please enter your name here