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.
var1='hello' print(var1) print(var1[1:3])
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.
a='hello how are you.' print(len(a))
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 print(count)
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.
a='hello' b='world' c=a+' '+b print(c)
If concatenation operation between different data type following error is appear.
a='hello' b=10 c=a+' '+b print(c)
Python String Method
There are various method are use in string. Some method are discuss here.
The lower method is used to convert string to lower case. Here is an example of a lower method.
a='HELLO WORLD' print(a.lower())
output: hello world
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' print(a.upper())
output: HELLO WORLD
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 ' print(a.lstrip()) print(a.rstrip()) print(a.strip())
output: Hello Hello Hello
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.' print(a.find('hello'))
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.' print(a.replace('hello','hi'))
output: '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)
output: True True