CodeGym /Courses /Python SELF EN /Strings as Arrays

Strings as Arrays

Python SELF EN
Level 10 , Lesson 2
Available

10.1 Looping Over String Characters

In Python, strings are often seen as arrays/lists of characters, making them super convenient for manipulations similar to working with arrays in other programming languages.

Since a string is a bunch of characters, you can loop through them.

Looping Over String Characters

To iterate over each character in a string, you can use a simple for loop. This allows you to perform operations on each character individually:


text = "Hello"
for char in text:
    print(char)

It will print:
H
e
l
l
o
            

Important! In Python, there's no char type for a single character. One character in a string is still a string.

10.2 Determining String Length

What else can you do with a string? Well, obviously you can determine its length — find out how many characters it has. For this, Python has a special built-in function len(). This function returns the number of characters in the string:


text = "Hello, world!"
length = len(text)
print(length)  # Output: 13
        

10.3 Accessing a Specific Character

Also, you can access a specific character in a string by its number. Or better said, by its index.

Important! Indexes for lists and arrays in Python start at 0. If a string has 10 characters, they will have indexes: 0, 1, 2, ...9.

The general way to access a character by its index looks like this:


string[index]

Let's print all characters of a string by accessing them through their indexes:


text = "Hello" 
for i in range(len(text)):
    print(text[i])
                            

It will print:
H
e
l
l
o
            

10.4 Substring Inclusion

Checking if one string includes another is one of the basic tasks you often need to solve when programming in Python. This feature is widely used in many applications, including text data processing, user input validation, and data searching within strings.

For this task, Python provides several ways, both unique to Python and common in other programming languages:

in operator:

This is the simplest and most common method to check if a substring is in a string. It returns True if the substring is present, and False otherwise.


text = "Hello, world!"
print("world" in text)  # Output: True
        

Method find():

The find() method returns the index of the first occurrence of the substring in the string, if found, and -1, if the substring is absent. This allows not only to check for the presence of a substring but also to find its position.


text = "Hello, world!"
position = text.find("world")
print(position)  # Output: 7
        

Method index():

Similar to find(), but instead of returning -1 when the substring is absent, the index() method raises a ValueError exception. This method is useful when the absence of a substring is unexpected and should result in an error.


text = "Hello, world!"
try:
    position = text.index("world")
    print(position)  # Output: 7
except ValueError:
    print("Substring not found.")
       

Method count():

The count() method counts how many times a substring appears in a string. This is useful when you need to know not only the fact of inclusion but also the number of occurrences.


text = "Hello, world!"
cnt = text.count("l")
print(cnt)  # Output: 3
       
2
Task
Python SELF EN, level 10, lesson 2
Locked
Substring search.
Substring search.
2
Task
Python SELF EN, level 10, lesson 2
Locked
Characters in a string
Characters in a string
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION