Extracting a piece of String in python

by anupmaurya
429 views

We’re going to see strings of various kinds and various lengths and we’re going to want to extract pieces of them, okay? And so the idea is to somehow get this part out and then convert it to a floating point.

Suppose you have given a string

text=”python is my language version : 2.38″

and you have find the postion of the ” : ” in the string

ipos =text.find(‘:’)
print(ipos)

As output you get the position of the “:” in the string.
that at 31 postion.

Now, if you have to print any piece of the code, Suppose you have to print “2.38” part of a text string, then

piece=test[ipos+1:]
print(piece)

this will give you the part of the string .
now if you also want to convert it into floating point .
So,we here use type casting.

print(float(piece)).
This is how you can extract a piece of string.

Happy Programming;)

You may also like