Course Content

String Slicing And Other Functions In Python

Strings

String is a data type in Python. Strings in Python programming language are arrays of bytes representing a sequence of characters. In simple terms, Strings are the combination or collection of characters enclosed in quotes.

Primarily, you will find 3 types of strings in Python :

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

Let us now look into some functions you will use to manipulate or perform operations on strings.

len() Function : This len() function returns the total no. of characters in a string. E.g. for string a="abc", len(a) will return 3 as the output as it is a string variable containing 3 characters

Strings are one of the most used data types in any programming language because most of the real-world data such as name, address, or any sequence which contains alphanumeric characters are mostly of type ‘String’.

E.g. Consider this string variable x

x = "String Demo"

This string variable x contains a string containing 11 characters (including spaces). Since the index in a string starts from 0 to length-1, this string can be looked at as:

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

String Slicing :

As we know the meaning of the word ‘slice’ is ‘a part of’. I am sure you have sliced paneer cubes at home!
Just like paneer slice refers to the part of the paneer cube; 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 the name of the string followed by [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. Let's look into an example!

 

In Python, string slicing s[n:m] for a string s is done as characters of s from n to m-1. It means characters are taken from the first index to second index-1.
For E.g. abc="Demo" then abc[0:3] will give ‘Dem’ and will not give ‘Demo’ coz index number of ‘D’ is 0, ‘e’ is 1, ‘m’ is 2, and ‘o’ is 3. So it will give a 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 need to give a skip value i.e. string[n:m:skip_value]. This simply takes every skip_valueth character. 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. Have a look at the example below:

 

Let's end this tutorial by looking into some of the most used string functions :

  • string.endswith(): This function allows the user to check whether a 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 an argument.

  • string.capitalize(): This function capitalizes the first character of any string. It doesn’t take any argument.

  • string.upper(): It returns the copy of the string converted to the uppercase.

  • string.lower(): It returns the copy of the string converted to lower case.

  • string.find(): This function finds any given character or word in the entire string. It returns the index of first character from that word.

  • string.replace(“old_word”, “new_word”): This function replaces the old word or character with a new word or character from the entire string.

I hope you enjoyed this tutorial. If you have any questions, make sure to post it in the QnA. Let us now move into the next topic Lists, which is also one of the most used data types in the python programming language

Code file as described in the video

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

print(mystr.endswith("bdoy"))
print(mystr.count("o"))
print(mystr.capitalize())
print(mystr.replace("is", "are"))

Comments(19)

Raheel123 1 year, 3 months ago
it's really amazing !
SachinJ 1 year, 2 months ago
Thank you so much brother.
sadia 1 year, 1 month ago
sourec code khn h? video ka
ahmedraza 9 months, 3 weeks ago
nice
ahmedraza 9 months, 3 weeks ago
awesome
wasi1978 8 months, 3 weeks ago
well done bro nice work
saswata 8 months ago
thank you sir
anubhavs921 8 months ago
Its very Helpful and best explain sir.... Thank yo
nazishali 7 months, 3 weeks ago
str1 = "Nazish {lname} is reading {object}".format(lname = "Ali", object = "book")
nazishali 7 months, 3 weeks ago
in the profile picture, there is no option to upgrade picture.
sanayasingh 7 months, 2 weeks ago
demo="Akash is a good boy"
print(demo.count(""). 
output=20
can you explain why the output is 20?
aarzoo 2 weeks, 5 days ago
did uh found out d ans?
Vivek-Mishra 5 months, 1 week ago
Full Notes and Source Code: https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes
Vivek-Mishra 5 months, 1 week ago
Remaining Codes will be updated Soon. Thanks, Harry Bhai for Awesome Tutorials
ankit100 3 months, 1 week ago
Sir what is source code in python?
sk2006 1 month, 3 weeks ago
its fully GREAT, in free everything is there which a paid course have. PLZ subscribe his youtube channel, so he is motivated and is able to provide more free content to us.
Thanks carry bhai
sk2006 1 month, 3 weeks ago
sorry, it will be HARRY BHAI
anigupta 1 month, 2 weeks ago
Harry bhai ek doubt tha ki if string would be "Harry is a is coder"  and if we want to replace the second "is" of the string with awesome , how will we do it because replace replaces both the "is" in the code. Please suggest 
ayush9989 6 days, 19 hours ago
Very nice sir
I am in 8th and i have 1 year experience of programming 
With you
Thank you sir

Resources

  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