Sitemap
Tehreem

Tehreem

Hey everyone! I'll be documenting my cybersecurity journey here. What I learn, what I understand. Feel free to reach out and tag along :)

Working with Strings (Python)

3 min readAug 25, 2026

--

Today I’m covering string operations in Python.

What is a string?
Converting other data type objects into strings
String operations
String indices and slices

Press enter or click to view image in full size

1- What is a string?

An ordered sequence of characters. For example, "123", "Ahmed”, etc. They are surrounded either by double quotes "" or single quotes ''.

2- Converting other types into strings:

Other data types, such as integers or floats, can also be converted to a string using the str() function.

Get Tehreem’s stories in your inbox

Join Medium for free to get updates from this writer.

The reason we convert objects into a string data type is that strings can have many functions and options that the integer data type does not have. The following example converts the integer input into a string.

#converting an integer into a string
new_string=str(123)
#to check the type of 123
print(type(new_string))

3- String operations:

Apart from the str() function, various other operations can be performed on strings. A few of them are explained below:

len(): number of elements in an object

+: string concatenation, joining two strings together

.upper() and .lower() methods

Press enter or click to view image in full size

4- String indices and slices:

What is a string index?

An index in a string means the position of each character.
For example, the string “Hello” has 5 characters, each with its own position. Because Python uses 0-based indexing, the indices range from 0 to n-1 (0 to 4). So the last character ‘o’ has the index number of 4.

String slicing:

String slicing is where you can specify which part of the string you want as an output. There is a starting index and an ending index. The starting one is included in the output, but the ending index is not.

Here is a breakdown of this output, recall that the index starts with 0,


[0]- H
[1]- E
[2]- L
[3]- L
[4]- O

The slicing index was [1:4]
‘e’ is at index 1, and was included in the output, but ‘o’ at index 4 was excluded.

Searching through the string:

To search for a specific character's index in a string, we use the .index() method.

But what if a string has more than one character? Let’s find the index of ‘l’:

The first time the letter l appears is at index 2, so it only prints the first occurrence of it. This was a short and simple explanation of the string operations.

Next up, I’ll be working with lists and list slicing. See you there :)

--

--

Written by Tehreem

Hey everyone! I'll be documenting my cybersecurity journey here. What I learn, what I understand. Feel free to reach out and tag along :)