Finding Number of Words

We have len() function that returns the number of characters in a string. Suppose we want to find the length of a string without using len() function, we can use a for loop as:

i=0
for s in str:
i+=1

This for loop repeats once for each character of the string 'str'. So, if the string has 10 characters, the for loop repeats for 10 times. Hence, by counting the number of repetitions, we can find the number of characters. This is done by simply incrementing a counting variable 'i' inside the loop as shown in the preceding code.


A Python program to find the length of a string without using len() function.


A Python program to find number of words in a given string:


Inserting Sub String into a String

Let's take a string with some characters. In the middle of the string, we want to insert a sub string. This is not possible because of immutable nature of strings. But, we can find a way for this problem.

example image for python finding words

A Python program to insert a sub string in a string in a particular position.