Python for everybody | 8 | String manipulation in Python programming

Python Tutorial 8
String Manipulation And Typecasting (1)

In this tutorial, we will cover Part 1 of String Manipulation and Typecasting.

Without further ado, let’s get started!

String Manipulation In Python Programming


Let’s start with string manipulation.

In Python, you can easily repeat a text by using the multiplication sign.

String manipulation in Python programming | 1

print(“Hi” * 3) will print HiHiHi.

print(“*” * 10) will print **********.

Joining Text

You can also concatenate text to join them together by using the plus sign.
String manipulation in Python programming | 2

print(text1 + “ “ + text2) will print I am here.

Alternatively, you can use a comma to concatenate text.

print(text1, text2) will print exactly the same thing.

The difference is that when using a comma to concatenate, Python automatically inserts a space between the 2 texts.

Printing Strings

To print a string, you can also use Python’s string formatting or what is called f-strings.

String manipulation in Python programming | 4

Python’s f-strings start with the f character followed by what you want to print enclosed within an open quotation mark and a close quotation mark.

If you want to include printing a variable inside your string literal, you just need to enclose your variable within an open curly bracket and a close curly bracket.

String manipulation in Python programming | 5

print(f’Hi, {text1} {text2}’) will print Hi, I am here.

Using f-strings, your code looks more elegant and readable.

Example 1

We can extract any portion of a text by referencing its index position from left to right.

The first index position from the left is always index 0 and not 1. Use the open and close square brackets to enclose the index. Indexes are always enclosed by an open and a close square bracket.

String manipulation in Python programming | 6

print(text[0]) will print uppercase H which is the first character of the string literal “Hello Jack”.

print(text[4]) will print lowercase o which is the 5th character of the string literal “Hello Jack”.

Example 2

We can also extract any portion of a text by referencing its index position from right to left instead.

The first position from the right is index -1.

print(text[-1]) will print lowercase k which is the first character of the string literal “Hello Jack” counting from the right.

print(text[-9]) will print lowercase e which is the 9th character of the string literal “Hello Jack” counting from the right.

Positive index positions start from the left and start with 0, 1, 2, 3, 4, 5, and so on ….. while negative index positions start from the right and start with -1, -2, -3, -4, -5, -6, and so on …..

For the same string, you can either use the positive indexes or negative indexes.

Example 3

To extract the first 5 characters, put a colon before the index.

print(text[:5]) will print the first 5 characters, which is Hello.

Example 4

To extract the last 3 characters, put a colon after the index and use negative.

print(text[-3:]) will print the last 3 characters, which is ack.

Example 5

We can also slice a string literal by putting a starting index followed by the colon and then putting an ending index.

Take note that the starting index is inclusive while the ending index is exclusive.

print(text[6:10] will print Jack because starting index position 6 is the uppercase J character and ending index position 10 minus 1 is the lowercase k character.

This means that if the ending index of the slicing is 10, the last character to be included in the slicing is not 10 but is 10 minus 1 — because the ending index position is exclusive.

Take careful note that when determining the last character to be included in the slicing, it is always the ending index position minus 1.

Example 6

If we want to slice by counting from right to left instead, we can use the negative sign for the index.

Note that again the starting index is inclusive while the ending index is exclusive.

print(text[-9:-6] will print ell because starting index position -9 is the lowercase e character and ending index position -6 minus 1 is the lowercase l character.

Example 7

We can also slice a string literal by putting only a starting index followed by the colon and not putting an ending index.

print(text[4:] will print o Jack because starting index position 4 is the lowercase o character and since we do not put an ending index, it will include until the last character of the string literal which is the lowercase k character.

Example 8

If we want to slice by counting from right to left instead, we can use the negative sign for the index.

print(text[-4:] will print Jack because starting index position -4 is the uppercase J character and since we do not put an ending index, it will include until the last character of the string literal which is the lowercase k character.

Watch my online Python course for a different learning experience.

>> Click here to watch my online Python course at Udemy