Python String Format

In this article, you will learn about python string format. Python String is a sequence of character. In python, any character inside single and double quotes is a string. This is a part of python data structure. If you are new and want to learn python basic here is a link. Now let’s discuss different terminology use in a python string.

Looking Inside Python String and Slicing

we can access every single character inside python string format through an index. The value of the string is starting at zero. For example, if word length is 5 then the index value is range from 0 to 4. You will get index error if you attempt to index beyond end of the character. You can also apply a range of index for slice character. Here is some example.


String Function

Python provide some in-built function for strings. Here we discuss about len, reverse, str and join functions. In python string len function is use to calculate total number of character in string. Reverse function is using for reverse string. And str is used to convert any character into string format. With help of join function flexible way to concatenating string. With help of join function any character are join to string. Following code show some python function use for string operations.

Python String Function
a='hello how are you.'

Looping Through String

On python String, we can also apply the python loop. When we apply a loop on string loop is iterate on every character of the string. For better understanding how does it’s work let’s make a simple program that counts the occurrence of the character ‘a’ on a given. This is a simple loop that loops through each letter in the string and counts the number of time character is encounters. Here is a code for this problem.

word = 'nepal is a beautiful country'
count = 0
for letter in word :
    if letter == 'a' : 
       count = count + 1

String Concatenation

concatenation operation allows between the same data type. In string concatenation operation concatenate multiple strings into a single string. Following code show string concatenation in python.

c=a+' '+b

If concatenation operation between different data type following error is appear.

c=a+' '+b
concatenate error

Python String Method

There are various method are use in string. Some method are discuss here.

  • lower()
    The lower method is used to convert string to lower case. Here is an example of a lower method.
hello world
  • upper()
    Like the lower method, it’s also used for case transform. Upper method help to translate capital letter to small letter. Here is an example of the upper method use in python programming.
a='hello world'
  • Strip()
    Strip method is used for removing space from the text. Lstrip and rstrip are part of the strip method. Lstrip method is used to remove left side white space. And rstrip is using for removing right side white space. In general, the strip method is used for removing both side white space. Following code show strip method in python programming.
a='   Hello    '
  • find()
    find method is used for search substring from the given string. This method finds the occurrence of substring. If a substring is not found then it returns -1. Following code illustrate find method.
a='hello how are you.'
  • replace()
    Replace method is like search and replace method using in a word processor. It replaces all the occurrence of the search string with a replacement string. Here is an example of the replace method in python programming.
a='hello how are you.'
'hi how are you.'

Python String Operation

In keyword is used as a logical operator. The in keyword is using to check whether the string exists in other string or not. The in expression is a logical expression that returns either true or false. it’s used in if statement. Following program show this concept in python program.

a='hello how are you.'
print('y' in a)
print('hello' in a)
print('hi' in a)


by using all the concept this python string format and previous articles let’s do one simple character count project. The overview of the project is input sentence from the user and also take input for key what user want to count. And count this character and display it.

text=input('Enter text.... ')
key=input('Enter Character you want to count')
for i in text:
    if i==key:
print('the character '+key+' in your sentense have',count)
final output


In this article we learn about python string format from basic to advance. In summery here is list what we learn from this article.

  • Looking Inside Python String and Slicing
  • Looping Through String
  • String Concatenation
  • Python String Method
    • lower()
    • upper()
    • strip()
    • find()
    • replace()
  • String Operation
  • Project


Ok, this is the end of the articles I hope you you can get a good lesson what I deliver in this article. I ask forgiveness for any word and behave which are not to be. If you want to do a project on data science and machine learning here is link of real-time face mask detection using OpenCV. Thank you for your kind and attention guys. Stay tuned for the next article. If you have any question regarding this article please feel free to comment below.

Leave a Reply