Strings in python – Part 2

Hello Techies!!!

Let’s recap a little bit, we learned about what strings in python are, how can we print a string, join two or more strings, print length of a string, print character at specific indexes in a string.

Today, we will learn more about string manipulation which will include changing a string to lowercase, changing a string to uppercase, getting parts of a string in different ways, splitting a string.

Let’s get right into it.

Changing a string to lowercase and uppercase:

We can use an in-built function ( function provided by python ).

Let first_name = ‘PiYuSh’ be a string literal.
to change it to lowercase,
first_name.lower()
to change it uppercase,
first_name.upper()

lower and upper strings example

Removing whitespaces ( blanks, spaces ) from start and end of a string:

example = ” Hello, World   “

As you can see there are few spaces present at start and end of the string. To remove this, we can use a function named strip().

string strip example.JPG

Splitting a string into different parts:

example = “Hello, World”
another_example = “My name is Piyush Agarwal”

We have two string literals with us, let’s see how can we split them using a delimiter.
You can understand a delimiter by looking at it as something which is separating two strings.

split strings example.JPG

Here, when we wrote split_example variable as example.split(), what we are asking python to do is call split function on a variable named as example. In turn, python is splitting “Hello, World” into two parts using comma as a delimiter and creating a list for us with two different strings to use – “Hello” and ” World”

Similarly, if we are splitting second string keeping delimiter as space. and in turn, it gave me 5 different string which were separated by single space.

Replace a character or part of a string:

The replace() method replaces a string with another string.

replace method string example.JPG

This function or method can be use to replace a single character or group of characters directly.

Splitting different parts of string :

 

string manipulation - part 2

Example above shows how we can get different parts of the strings ( or slice a string ) and use it to our advantage. Let’s break down a bit,

print(name) or print(name[:])

Both of them can be used to print out complete string.

print(name[1:4])

Here, a number before colon denotes starting index of sub-string ( part of a string ) and number after colon tells us ending index of the string ( excluding that index value ). If not mentioned, default starting index is 0 and default ending index is end of the string.

What if you want to print alternate character? or reverse a string?

string manipulation - part 2.2

Example above shows something called extended slicing of a string.

The syntax is:
seq[ start : end : step ]

seq[::n] is a sequence of each n-th item in the entire sequence.

That’s it for strings. There is lot of information shared with you guys today, be patient and take time to understand and practice strings in python to get better hands-on.

If stuck somewhere, feel free to ask questions in comments and I will try to get back to you as soon as possible.

Have a great and wonderful day.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.