String slicing and other data types

String slicing

String is nothing but it's just a collection of the characters & data type of a python, it is important than other data types a lil bit, because everything is most of type is the string, like document, sentences and many more.
    In python the starting of an index is started with the [ 0 ], means in every statement of the string the first letter of the first word is must be 0 then 1, 2, 3.....n, 

Slicing of the string 
code👇
mystr = "i am a good boy"
print(mystr[
5])
output👇
a

here print "a" as a output because in this statement "a" varies on 5th location .

If we want to take a small part of the sentence from the 0 index to 4.

code👇
mystr = "i am a good boy"
print(mystr[0:4])
output👇
i am

note: if the given ratio is more then the sentence of the string, it prints the whole sentence the variable   whereas, the first ratio is must be "0" by default.

If we want to check the length of the string, use "len" function
code👇
mystr = "i am a good boy"
print(len(mystr))
output👇
15
To print the variable sentence in reverse, we can use negative (-) sign.
code👇
mystr = "i am a good boy"
print(mystr[-4:])
output👇
 boy

it prints the boy first with the 4th number of the tab.
here is a method idea for the index slicing.

 0  1  2  3  4  5  6  7  8  9
 A  B  C  D  E  F  G  H  I  J
-9 -8 -7 -6 -5 -4 -3 -2 -1  0

Some Slicing functions

(i)

code👇
mystr = "i am a good boy"
print(mystr.isalnum())
output👇
False

In this function it shows the output false because here is a white spaces between the words in the sentence.

(ii)

code👇
mystr = "i am a good boy"
print(mystr.isalpha())
output👇
False

Its also a false because its not a numerical variable.

Some more functions

code👇
mystr = "i am a good boy"
print(mystr.endswith("boy"))
print(mystr.count("o"))
print(mystr.capitalize())
print(mystr.lower())
print(mystr.upper())
print(mystr.find("good"))
print(mystr.replace("good", "bad"))
output👇
True
3
I am a good boy
i am a good boy
I AM A GOOD BOY
7
i am a bad boy

note: for more functions click here👉   https://www.w3schools.com/python/python_ref_string.asp




Comments

Popular Posts