Python Tutorial 4---"String Slicing And Other Functions In Python"

 Python Tutorial 4---

"String Slicing And Other Functions In Python"

This PROGRAM or TUTORIAL about Python's  "String Slicing And Other Functions In Python"

DOC

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 in the entire string.

CODE

#String Slicing And Other Functions In Python
mystr = "Saswata is a good boy"
print(mystr[0:7]) #Output =Saswata
"""Python's Index starts with 0, Example
Saswata is a good boy - here 0=S, 1=a, 2=s, 3=w, 4=a, 5=t, 6=a, 7= (blank/space)
if we want to print just one alphabet of the string we can use print(mystr[any single number])
but if we want print to print just a word instead of the whole string, we'll use
print(mystr[0:7]), it'll print my name. Output = Saswata
We can't use 6 in here beacuse the last num always excludes in strings
if we use 6 like--- print(mystr[0:6]), Output = saswat"""
#if we want print length of the string, we use len function, example---
print(len(mystr)) #Output=21 [as it starts from 0 so it's 21, if it starts from 1 the output would 20]
print(mystr[0:50]) #Output=Saswata is a good boy
#Yes, we can write big excl num if we want,
# but output will be the same amount of number as put in our variable "mystr"
print(mystr[::]) #Output=Saswata is a good boy
"""print(mystr[::]) here [its always 0:its the length:if its blank its always 1]
example --- [::] and [0:21:1] both are same.
[::2/3/5] if we put more than 1 in the last sec it will skip that times of nums.
It's called 'EXTENDED SLICING' """
print(mystr[-4:-2]) #Output= b
#It's negative index if we want to print our string from backwards, we can use that.
print(mystr[::-1]) #Output=yob doog a si atawsaS
#it'll print backwards,
#if I put -2 instead -1, first it'll print it all backwards then skip the chars.
print(mystr[::-2]) #Output=ybdo iaasS
print(mystr.isalnum()) #Output = False
#it's false because it's not an AlphaNumeric string, i has spaces, if we remove them it'll be true.
print(mystr.isalpha()) #Output = False
print(mystr.endswith("boy")) #Output = True
#it's true because the string does end with 'boy', if I put some other word like 'ball' the it'll be false.
print(mystr.count("a")) #Output=4
#counts function counts the number chars in the strings
print(mystr.capitalize()) #Output = Saswata is a good boy
print(mystr.replace("is", "are")) #Output = Saswata are a good boy
print(mystr.upper()) #Output = SASWATA IS A GOOD BOY
print(mystr.lower()) #Output saswata is a good boy

Comments

Popular posts from this blog

Python Exercise 7 --- "Snake, Water, Gun"

Python Tutorial 38 --- "Self & __init__() (Constructors)(OOPS 3)"

Python Exercise 4 --- "Number Guessing Game"