Python String Functions

With help of python string functions we can manage string and modify it as per requirement. Example, we can capitalise a character in string or we can lower it. We can split a string in format or get some values from it. Searching a keyword in string and etc.

split Function

With help of this function we can split our string in multiple parts. e.g

string1 = "This is main script"
str1, str2, str3, str4 = string1.split()
print ("str1 is %s \nstr2 is %s \nstr3 is %s \nstr4 is %s" % (str1,str2,str3,str4))
Output:

python split function | Python string function

How did it split ?

Python split function check for any bank space in string and split it from their.

I wan to split based on something else.

You can do that e.g if you want to split string based on a word “yo” you can use function like.

 str1, str2, str3 = string1.split("yo")

Length of string

We can use function len to get length of string. Length is always 1 less then you count, Why? because python start counting from 0 (Zero). So, if you count “Akar” you count 4 character (1-4) but python count it as 3 (0-3). e.g

print (len(string1))

or

var1 = len(string1)

string or array?

Python read every string as array. i.e if your string is

string1 = "This is main script"

then very first character of this string will be “T” and last will be “t” and second last will be “p”. You can print any character of string with help of brackets []. Python start counting of array from 0 (zero), so h will be first character and T will be zeroth character.

e.g Copy and past this code

string1 = "This is main script" 
last = len(string1) 
# Printing single character
print ("forth character is \"%s\" which is at 3rd position" % (string1[3])) 
print ("That's because counting start from Zero") 
# Printing multiple characters starting from 5 to 12
print ("Printing values from 5 to 12 \"%s\"" % (string1[5:12]))
#Printing last 6 character
print ("Printing last 6 character \"%s\"" % (string1[last-6:last]))

Explanation of code

  • First thing I am not going to explain why I used \” in code (check previous chapters, you have to go a lot back, or search for escape in search box).
  • you can pass start and last array values to print with “:”.
  • you can use string1[8:] this means values starting from 8 to last. or string1[:10] means values starting form 0 to 10.

output

Count string in string, count() function

You can use count function for counting a string in string e.g

string1.count("s")

Find position, find() function

Python can find a string in string. You can do that with help of find function e.g

string1 = "This is main script"
value = string1.find("is")
print (value)

I want you to try this code and see the output. Output will be 2? surprised ??? well thats because word “This” contain “is“.  What if I want to find 2nd value of “is

simple,

string1 = "This is main script"
value = string1[4:].find("is")
print (value + 4)

This will find values in string1 starting from values 4 to last. Now remember this value will be 4 less then it should be, thats because we have supplied string with 4 less character. Try below script

string1 = "This is main script"
value = string1[string1.find("is")+2:].find("is")
print (value + 4)

This will return same value.

 

no comments

Leave me comment