ring 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 False
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') count=0 text=text.lower() key=key.lower() for i in text: if i==key: count+=1 print('the character '+key+' in your sentense have',count)
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
- String Operation
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.