Python

String manipulation in python

1 min read

In this article you would learn basics of string manipulation with Python. To understand it better lets have a look at the table below:

STRING P Y T H O N
POSITION 0 1 2 3 4 5

As shown in the table, our string is “PYTHON” and each of the character is positioned from 0 to 5 (note the numbering starts from zero). Now we can do lot of things with this string using python code. Here is some of it for you to get going, have a look. Each line of code is followed by a # or comment explaining what each line of code does.

x='PYTHON' #assign the string “Python” to variable x
length_of_string = len(x) #gives the length of the string
print ('No of characters in string PYTHON are:')
print (length_of_string) # print the value of length_of_string
#string slicing: take part of string out
print (x[1:4]) # starting from 1st till 4th but excluding 4th
print (x[2:]) # starting from 2nd till end
print (x[:4]) # starting from start till 4th but excluding 4th
print (x+ " is awesome !") # string concatenation

Try yourself now, copy the code in blue font above and paste in the code window below and then press run. Feel free to experiment with the code.

Let us know in the comment section below if you have any doubts.

Keep learning !!