Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about how to find Character in a String in Python. Here string there are character which all have specific space. In this tutorial we will learn about How to find Character in a String in Python. so without wasting time lets learn about of this.
How to find Character in a String in Python
find Character in a String in Python
To find Character in a String in Python Just use find() we cam fine the first position of the character. Lets learn this by given below example.
str1 = 'My name is Happy' f1 = 'y' print(str1.find(f1))
Output :1
How to find Character in a String in Python
To find Character in a String in Python Use find() we cam fine the first position of the character. Lets learn this by given below example.
str1 = 'My name is Happy' f1 = 'y' print(str1.find(f1))
Output :1
By using rfind() we cam fine the last position of the character. Lets learn this by given below example.str1 = 'My name is Happy' f1 = 'y' print(str1.rfind(f1))
Output :15
python find character in string
To find Character in a String in Python Use find() we cam fine the first position of the character. Lets learn this by given below example.
str1 = 'My name is Happy' f1 = 'y' print(str1.find(f1))
Output :1
By using rfind() we cam fine the last position of the character. Lets learn this by given below example.str1 = 'My name is Happy' f1 = 'y' print(str1.rfind(f1))
Output :15
Method 1: Using Find()
By using find() we cam fine the first position of the character. Lets learn this by given below example.
str1 = 'My name is Happy'
f1 = 'y'
print(str1.find(f1))
Output :
1
Method 2: Using index()
By using index() we cam fine the first position of the character. Lets learn this by given below example.
str1 = 'My name is Happy'
f1 = 'y'
print(str1.index(f1))
Output :
1
Method 3: Using rfind()
By using rfind() we cam fine the last position of the character. Lets learn this by given below example.
str1 = 'My name is Happy'
f1 = 'y'
print(str1.rfind(f1))
Output :
15
Method 4: Using for loop
By using for loop you can find each and every position of the character. So it is very useful and easy to use. So lets learn this by given below example.
str1 = 'My name is Happy'
f1 = 'y'
list = []
for pos,char in enumerate(str1):
if(char == f1):
list.append(pos)
print(list)
Output :
[1, 15]
Conclusion
It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?
Also, Read