Arastoo Dev
Arastoo Dev Blog

Arastoo Dev Blog

Python String 101

Python String 101
Arastoo Dev's photo
Arastoo Dev

Published on Sep 24, 2021

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Python strings are really powerful tools for working with texts. It’s very important to have some features in your language that helps you to work with text and luckily python has this power. Alongside the default features that python offers, there are some libraries that help you to develop your control on text.

Strings 101

The simplest way to use a string is the formal way when we write our first application with a new language: Hello World! In python to show a string or any other sort of output, we use the print method.

print("Hello World!")

As you can see we use "Double Quotations" to use string data type. We can also use 'Single Quotations.

print('Hello World!')

Using Variables

We can store a string in a python variable. To doing this we just assign a string to a variable.

name = 'Arastoo'

Normally in those sorts of languages that are not Dynamic Type, we should define the type of variable. But as we should know, Python is dynamic typed, so we don’t need to declare the data type. Python as a dynamic typed automatically managed this.

Multi-line strings

Well, we can print any text but when we want to show a multi-line text how we can do this? We can use three double quotations for this reason.

about_me = """ I'm a Python developer but it does not mean that I can't understand other languages.
I have a lot of potentials to learn anything.
I started programming with C-Sharp at college but I've changed my mind and get myself a little challenge to work with Linux OS and C as a new programming language.
It's been 5 years since that time.
"""

As I mentioned earlier we can use single quotations for this situation too.

Strings as Arrays

Strings in Python can be iterated just like lists or arrays. So we can access the characters with indexing. Look at this example:

name = 'Arastoo'
print(name[0])

This will print ‘A’ character. Think name variable as a row with 7 columns and you can access columns data using 0-7 numbers. Why 7? Because 7 is the latest column in ‘Arastoo’ string. For reading more on arrays and accessing with Index you can read ‘ Indexing in Python – A Complete Beginners Guide ’ post.

Looping in strings

As we said before, strings are a sort of list. The main aspect of a list or an array is iterating features. We can iterate a string with a ‘for loop'.

name = 'Arastoo'
for character in name:
   print(character)

In the output, we can see that each character will be printed in different lines. Even if we have a space, this space will be a separate character. Take this for example:

name = 'Arastoo Dev Abasi'
for character in name:
   print(character)

Existence Checking

We can check the existence of a character, word, or phrase in a string. This simply implements by using an if statement. The output will be a true/false value which we called boolean. We make decisions based on this value. For example, if there is a specified word then print or alert something.

about_me = """ I'm a Python developer but it does not mean that I can't understand other languages.
I have a lot of potentials to learn anything.
I started programming with C-Sharp at college but I've changed my mind and get myself a little challenge to work with Linux OS and C as a new programming language.
It's been 5 years since that time.
"""

if 'Python' in about_me:
   print('We found Python in the string')
else:
   print('There is no python in this string')

Remember: This checking process is Case-Sensitive. So ‘Python’ will be different from ‘python’.

Let use them all

Let think that we have a dictionary with a list of brands. Which are either cheap or expensive. We want to make sure we can access two different lists after we run an if statement. One for cheap and one for expensive.

brands = {
   'Apple': 'Expensive',
   'Microsoft': 'Expensive',
   'MSI': 'Expensive',
   'Xiaomi': 'Cheap',
   'LG': 'Cheap',
   'Acer': 'Cheap',
}

def detection(x):
   for brand in x.items():
       if 'Expensive' in brand:
           print("This brand is expensive:",brand[0])
       elif 'Cheap' in brand:
           print("This brand is cheap:", brand[0])
       else:
           print('Nothing')

detection(brands)

Well, this snippet is a little complex for newbies but I just want to use those things that we already learned in this blog post.

 
Share this