Day #12 - Strings Slicing and Operations on Strings

Day #12 - Strings Slicing and Operations on Strings

ยท

3 min read

Introduction

In the previous blog we learnt about strings in python and today I will share more on the string operations and looping through the string. I also wrote a bunch of code to practice the string functions and have attached the source code as we head towards the end.

So let's get started...

Rule 1:

A simple rule to keep in mind before we move further, do remember that in most of the programming languages index in strings starts from 0 and not 1

Length of a string - len()

Using len() function we can find the length of the string

Example -

fruit = "Mango"
len1 = len(fruit)
print("Mango is a", len1, "letter word.")
Output:
Mango is a 5 letter word.

String as an Array

The string is LIKE an array or sequence of characters. We can access elements in a string like an array.

pie = "ApplePie"
print(pie[:5])
print(pie[6])    #returns character at specified index
Apple
i

Note: This method of specifying the start and end index to specify a part of a string is called slicing.

Slicing example -

pie = "ApplePie"
print(pie[:5])      #Slicing from Start
print(pie[5:])      #Slicing till End
print(pie[2:6])     #Slicing in between
print(pie[-8:])     #Slicing using negative index
Apple
Pie
pleP
ApplePie

Loop through a String:

Strings being like arrays and arrays being iterable, we can loop through strings in python.

Example -

alphabets = "ABCDE"
for i in alphabets:
    print(i)
A
B
C
D
E

Some more CODE

Examples

names = "Chintan, jain"

nameslength = len(names)
print(nameslength)
print(names[0:4]) #gives 4 characters of names i.e 0 to 3, i.e including 0 but not 4
print(names[:4]) #same o/p as above
print(names[1:4]) #gives 3 characters of names i.e 1 to 3 excluding 4
print(names[:]) #this gives full length

#negative slicing
print(names[0:9]) #below 2 statments are same as this one.
print(names[0:-4]) # this is equivalent to above statement
print(names[0:len(names)-4]) #meaning of above statement is this line

print(names[-1:len(names)-4]) # this does not print anything because this means [-1:-4] >> [12:8]. this line does not make any sense.
13
Chin
Chin
hin
Chintan, jain
Chintan,
Chintan,
Chintan,

Resources Used

You can watch the video of Day#10 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. With today's blog post, I hope you all learned something new, just the way I did. In a gist, we learnt about String slicing, length of string, string as an array, loops in string and wrote a bunch of code.

So this is it for Day 12 of my python coding journey.

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย