String Manipulation In Python

String Manipulation In Python

Hey python programmers, I am Shobhit , web developer also learning backend. Assuming that you know what are strings and its basic operations In Python, lets deep dive into strings and learn some awesome string manipulation techniques.

1. String Slices

In Python, string slice is a term which refers to a part of the string, where strings are sliced using a range of indices.

Syntax : string[n:m] , where n and m are integers. Python will return a slice of the string by returning the characters falling between indices n and m - starting at n,n+1,n+2,n+3......till m-1.

For example: Take a word , eg 'Shobhit' , lets do string slicing of it. This is the pattern of numbering in each word.

0 1 2 3 4 5 6 : S H O B H I T

-7 -6 -5 -4 -3 -2 -1 : S H O B H I T

Then, play wth your code :)

name = 'shobhit'
print(name[0:5])
print(name[1:5])
print(name[3:4])
print(name[-7:5])
print(name[-4:-1])

#Output
# shobh
#hobh
#b
#shobhi
#bhi

Note that: You can skip either begin-index (n) or last(m), python will consider the limits of the string i.e. for missing begin-index(M), it will consider 0 for first index and for missing last value , it will consider the length of the string.

Pro tip: You can use string[: : -1] to reverse a string.

2. len() function

It returns the length of its argument string, length, in the sense, characters present in that string.

name = 'shobhit'
print(len(name))

# Output: 7

3. count() function

It returns the integer of how many times a particular word, group of words have occured in the argumented string.

name = 'shobhit'
print(name. count('h'))

#output : 2

num = ' 121112111211'
print(num.count('1'))

#output: 9

4. find() function

It returns the lowest index in the string where the substring is found within the slice range of start and end. It will return -1 if substring is not found.

<string>.find(sub[,start[,end]])
string = 'Code in Python'
print(string. Find('Python'))

5. index() function

It returns the lowest index where the specified substring is found. If the substring is not found then an exception, Value error is raised.

It is similar to find(), but find() returns -1 if substring is not found, whereas index() will raise Value error if the substring is not found in string.

>>> 'abracadabra'. Index('ab')
0

You must have observed it returned 0, why ? index() or find() always returns the lowest index of the substring, if there are more than 1 indexes of same substring.

6. replace() method

As the name says, it will return a copy of the string with all occurences of old substring replaced by a new one.

#syntax
string.replace(old, new)
s=' I like Javascript'
print(s.replace('Javascript','Python'))

#output: I like Python

7. join() function

It joins a string or character after each member if the string iterator i.e. string based sequence.

#syntax
<string>.join(<string iterable>)
>>> '#'.join('Python)
P#y#t#h#o#n

8. capitalize() function

It will return as a copy of the string with its first letter capitalized.

#syntax
<string>.capitalize()
>>> 'shobhit'.capitalize
#output : Shobhit

9. title() function

It will return a copy of the string where all words will start with uppercase characters and other characters will be in lowercase. It is different from capitalize function because capitalize function returns with first letter in uppercase while title will make first letter uppercase for all words in the string.

s = 'hello world'
print(s.title)

#output : Hello World

Thats pretty much it for the blog. I hope this blog helped you. Follow me on github to view some of my work on Python programming. Click here for Github profile