Hello Guys ,
In previous tutorial you learned about variables , Data types , operators , type() function , typecasting and input() function in python.
In this tutorial you will learn below points:
- String
- String slicing
- String function
Let's get started.
String
- String is a sequence of characters enclosed in quotes.
- String is a data type.
- We can writing String in three ways.
Single Quoted String :
# --> Single Quoted String
a = 'Ps_Inf0'
print(a)
Double Quoted String :
# --> Double Quoted String
b = "Ps_Inf0"
print(b)
Triple Quoted String :
# --> Triple Quoted String
c = '''Ps_Inf0'''
print(c)
String slicing
- A String in python can be sliced for a getting a particular part of string.
- The index in a string start from 0 to (length - 1) in python. In order to slice a string , you can use the below syntax:
variable_name = Storing_value_variable_name[ start_index : last_index ]
- if you don't write starting index then python take index value 0(Zero).
- if you don't write last index then python take index (length - 1).
word = "infopsh"
# --> Return Character from 0 to 4
a = word[0:4]
# --> Negative Indices
b = word[0:-1]
print(a)
print(b)
Output :
info
infops
String function
len() function :
The len() function is used to find the length of the string.
word = "infopsh"
''' --> Here len() function is used to
find the length of the string '''
length = len(word)
# --> print the length
print(length)
Output :
7
endswith() function :
The endswith() function return true if the string ends with specified string values otherwise false.
sentence = "Hi I am Blogger.My Blog name is Ps_Inf0"
'''
The endswith() function return true if the
string ends with specified string values
otherwise false.
'''
print(sentence.endswith("Ps_Inf0"))
Output :
True
count() function :
The count()function return total number of occurence of any character.
sentence = "Hi I am Blogger.My Blog name is Ps_Inf0"
'''count()function return total number of occurence
of any character '''
print(sentence.count("i"))
Output :
2
capitalize() function :
The capitalize() function is used to capitalize(Uppercase) the first character of string.
sentence = "hi i am blogger."
'''capitalize() function is used to
capitalize(Uppercase) the first character of string.'''
print(sentence.capitalize())
Output :
Hi i am blogger.
find() function :
The find() function is used to find word and return the first index of that word in the string.
sentence = "Hi I am Blogger.My Blog name is Ps_Inf0"
'''find() function is used to find word and return
the first index of that word in the string.'''
print(sentence.find("Blogger"))
Output :
8
replace() function :
The replace() function is used to replace old string with new string in the entire string.
sentence = "Hi I am Blogger.My Blog name is ps_inf0"
'''
Here replace() function is used to replace
old string with new string in the entire string.
'''
print(sentence.replace("ps_inf0" , "Ps_Inf0"))
Output :
Hi I am Blogger.My Blog name is Ps_Inf0
If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support. 🙏
0 Comments