NO STRINGS ATTACHED

--Originally published at Coding The Future

Photo by Everton Vila

Hey everyone! It's been so long since my last post, but trust me, there's reasons for my absence and I am back with exiting stuff.

To begin today's topic, I have another of my quick stories... Lately, my life has been overtaken by discernment confusion on what I want to do, what I must do, and what is expected of me. I don't think a lot people go through this kind of crisis as often as I do, but I think we've all been there at some point in our lives. How is this relevant? Again, it really isn't, but I wish I could just try things sometimes without any strings attached.

Oh, well. That was deep. But now let's focus on strings, yay!

What are strings?

As you may know from my previous posts on types, one of the most interesting and common types in programming languages are strings.

Strings are a literally a chain of characters of any type. So any text-based variable is a string. Strings can include numbers, but these are not treated like numbers, rather like labels. So if you declare two strings like "2" and "4" and try to multiply them, you will get an error, because you can't multiply text! Get it? String is just plain text.

Declaring strings

Declaring strings in Python could not be easier. All you need to do is declare a variable and assign it a value within quotation marks. Take a look:

firstName = "Emanuel"

Now, we can do all sorts of things with this string! We can print it, we can concatenate it (join it with another string), split it, you name it!

How can they be used?

As I just mentioned, strings can be pretty powerful. I know I said you add or multiply "2" and "4". Well, kind of... Using operators like + and * will not give us a numeric result, but since string are quite powerful, we can use these to do cool things with them. Here's a list of a few things you can do with strings:

  • Concatenate: Concatenation means joining two string together. For example, let's say I had two strings, one that stores my first name and another one that stores my last name, but I wanted them to become one single string. I could join them by using the + operator:

    userName = firstName + lastName

    If we printed userName it would return EmanuelEstrada. If we wanted a space in between, we would need to concatenate a space in between the two variables.

  • Repeat: If for some reason we wanted to repeat the value of a string, it is possible through the * operator. Take a look:

    nameTwice = firstName * 2

    If we printed nameTwice it would return EmanuelEmanuel.

  • Slice: Strings, as I said before are like a chain of characters. Therefore, they can aso be seen as a list with a character in each index. Consequently, if we wanted to obtain a certain character in a specific spot or range, we could do something like this:

    userInitial = firstName[0]

    If we printed userInitial it would simply return E, because that is the first letter in the string.

Last few notes:

There are so many more amazing things you can do with strings. There are so many modules to help you split them, count how many words or characters they store, etc. We will probably touch upon those in an upcoming post. For the time being, I leave you with a tutorial by TutorialsPoint you may find useful.

Also, it is important to note that the type char does not exist in Python. A char is a variable that stores only one character, but in Python, we decided to just convert them into single-character strings.

Well, that's it for today... Hopefully these strings will help me forget about my existencial crisis. Until next time!

@emamex98

comments powered by Disqus