String Slicing And Other Functions In Python

Strings :

String is a data type in Python.

Strings in Python language are arrays of bytes representing Unicode characters. It means strings are the combination or collection of characters enclosed in quotes.

There are 3 type of strings in Python :

  • Single Quote String – (‘Single Quote String’)
  • Double Quote String – (“Double Quote String”)
  • Triple Quote String – (‘’’ Triple Quote String ‘’’)

len() Function : This len() function returns the total no. of characters in a string.

E.g. a=”abc” so len(a) function will return 3 as a variable contain 3 character string.

Strings are one of the mostly used data type in any of the programming language because whenever we need data such as name, address, or any data which contain alphanumeric characters then that data type is always ‘String’.

E.g. x=“String Demo” : This string variable x contains a string and in this string there are 11 characters (including spaces) so index of this string is from 0 to length-1 of string where length = 11. So S’s index no. is 0 and t is 1 and so on.

Note : The indexes of a string begin from 0 to (length-1) in forward direction and -1,-2,-3,…,length in backward direction. 

String Slicing :

As we know the meaning of word ‘slice’ is ‘a part of’. In the same way, in Python, the term ‘string slice’ refers to a part of the string, where strings are sliced using a range of indices.

To do string slicing we just need to put name of string [n:m]. It means ‘n’ denotes the index from which slicing should start and ‘m’ denotes the index at which slicing should terminate or complete.

In Python string slicing is done in such a way i.e. n to m-1. It means from given index to given index-1.

For E.g. abc=”Demo” then abc[0:3] will give ‘Dem’ and will not give ‘Demo’ coz index no. of ‘D’ is 0, ‘e’ is 1, ‘m’ is 2 and ‘o’ is 3. So it will give range from n to m-1 i.e. 0 to 3-1=2. That’s why we got output ‘Dem’.

In string slicing we sometimes give skip value also i.e. string[n:m:skip_value].

By default the skip value is 1 but if we want to choose alternate characters of a string then we can give it as 2.

String Functions :

  • string.endswith() : This function allow the user to check whether the given string ends with passed argument or not. It returns True or False.
  • string.count() : This function counts the total no. of occurrence of any character in the string. It takes the character whose occurrence you want to find as argument.
  • string.capitalize() : This function capitalize the first character of any string. It doesn’t take any argument.
  • string.upper() : It returns the copy of string converted to upper case.
  • string.lower() : It returns the copy of string converted to lower case.
  • string.find() : This function finds any given character or word in complete string. It returns the index no. of that character or word.
  • string.replace(“old_word”, “new_word”) : This function replaces the old word or character with new word or character in complete string.

Code file as described in the video

mystr = "Harry is a good boy"
# print(len(mystr))
# print(mystr[::-2])

print(mystr.replace("is", "are"))


Raheel123 10 months, 3 weeks ago
it's really amazing !
ahmedraza 4 months, 3 weeks ago
SachinJ 9 months, 4 weeks ago
Thank you so much brother.
sadia 8 months, 3 weeks ago
sourec code khn h? video ka
ahmedraza 4 months, 3 weeks ago
ahmedraza 4 months, 3 weeks ago
wasi1978 3 months, 3 weeks ago
well done bro nice work
saswata 3 months ago
thank you sir
anubhavs921 3 months ago
Its very Helpful and best explain sir.... Thank yo
nazishali 2 months, 3 weeks ago
str1 = "Nazish {lname} is reading {object}".format(lname = "Ali", object = "book")
nazishali 2 months, 3 weeks ago
in the profile picture, there is no option to upgrade picture.
sanayasingh 2 months, 2 weeks ago
demo="Akash is a good boy" print(demo.count(""). output=20 can you explain why the output is 20?
Vivek-Mishra 1 week, 6 days ago
Full Notes and Source Code:
Vivek-Mishra 1 week, 6 days ago
Remaining Codes will be updated Soon. Thanks, Harry Bhai for Awesome Tutorials


  1. File - Download here
  2. File - Download here
  3. File - Download here
  4. File - Download here
  5. File - Download here
  6. File - Download here

Course Announcements

Any Course related announcements will be posted here

Course Content