Skip to content

Commit 1255fab

Browse files
added coding solved questions for commit
0 parents  commit 1255fab

21 files changed

+263
-0
lines changed

10_Question.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# 10. Write a Python program that matches a letter at the beginning of a string.
3+
4+
import re
5+
6+
str1 = input("Enter String : ")
7+
8+
9+
res = re.findall(r'^\w',str1)
10+
print(res)

11_Question.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# 11. Write a Python program that matches a word at the end of string, with optional punctuation.
3+
4+
import re
5+
6+
7+
def matching(text):
8+
res = re.findall(r'\w\W$',str1)
9+
if res:
10+
return "Matched !!"
11+
else:
12+
return "Not Matched !!"
13+
str1 = input("Enter String : ")
14+
15+
print(matching(str1))
16+

12_Question.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 2. Write a Python program that matches a word containing 'z'.
2+
3+
import re
4+
5+
6+
def matching(str1):
7+
res = re.findall(r'\w*[z]{1}\w+',str1)
8+
if res:
9+
return "Matched !!"
10+
else:
11+
return "Not Matched !!"
12+
str1 = input("Enter String : ")
13+
14+
15+
16+
print(matching(str1))

13_Question.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 13. Write a Python program that matches a word containing 'z', not at the start or end of the word
2+
3+
from importlib.abc import ResourceLoader
4+
import re
5+
6+
def matching(str1):
7+
res = re.findall(r'\B[z]\B',str1)
8+
if res:
9+
return "Matched !!!"
10+
else:
11+
return "Not matched !!!"
12+
13+
str1 = input("Enter string : ")
14+
print(matching(str1))

14_Question.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# 14. Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores
3+
4+
import re
5+
6+
def matching(str1):
7+
res = re.findall(r'\w+_+',str1)
8+
if res:
9+
return "Matched"
10+
else:
11+
return "Not Matched"
12+
13+
str1 = input("Enter String : ")
14+
15+
print(matching(str1))

15_Question.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 15. Write a Python program where a string will start with a specific number
2+
3+
import re
4+
5+
def matching(str1):
6+
res = re.findall(r'^123',str1)
7+
if res:
8+
return "Matched !!!"
9+
else:
10+
return "Not matched !!"
11+
12+
str1 = input("Enter String : ")
13+
14+
print(matching(str1))

16_Question.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 16. Write a Python program to remove leading zeros from an IP address.
2+
3+
import re
4+
5+
def matching(str1):
6+
res = re.sub(r'\.[0]*','.',str1)
7+
return res
8+
9+
10+
str1 = input("Enter String : ")
11+
12+
print(matching(str1))
13+

17_Question.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 17. Write a Python program to check for a number at the end of a string.
2+
3+
import re
4+
5+
def matching(str1):
6+
res = re.findall(r'.\d$',str1)
7+
if res:
8+
return "Matched !!!"
9+
else:
10+
return "Not matched !!"
11+
12+
str1 = input("Enter String : ")
13+
14+
print(matching(str1))

18_Question.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 18. Write a Python program to search the numbers (0-9) of length between 1 to 3 in a given string.
2+
3+
4+
import re
5+
def matching(str1):
6+
res = re.finditer(r'(\d{1,3})',str1)
7+
return res
8+
str1 = input("Enter String : ")
9+
10+
ans = matching(str1)
11+
12+
for i in ans:
13+
print(i.group())
14+
15+

19_Question.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 19. Write a Python program to search some literals strings in a string.
2+
3+
import re
4+
5+
def matching(str1,name):
6+
pattern = ['fox', 'dog', 'horse' ]
7+
# res = re.findall(r'.fox ',str1)
8+
for word in pattern:
9+
if re.findall(word,str1):
10+
print("Got a Matched !!!")
11+
else:
12+
print("Not a match !!")
13+
14+
str1 = input("Enter String : ")
15+
name = "fox"
16+
17+
matching(str1,name)

1_Question.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# 1. Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9)
3+
4+
import re
5+
6+
str1 = input("Enter string : ")
7+
8+
res = re.findall(r'\w',str1)
9+
10+
res1 = ' '.join(res)
11+
print(res1)

20_Question.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 20. Write a Python program to search a literals string in a string and also find the location within the original string where the pattern occurs.
2+
3+
import re
4+
5+
def matching(str1,patterns):
6+
lst_str1 = str1.split('.')
7+
str1_new = ' '.join(lst_str1)
8+
lst2 = str1_new.split(' ')
9+
count = 0
10+
for pattern in patterns:
11+
matching = re.search(pattern,str1_new)
12+
if re.findall(pattern,str1_new):
13+
print(f"For {pattern} Got a Match !!")
14+
print(f"Position : {matching.start()} - {matching.end()}")
15+
else:
16+
print("Not a Match !!")
17+
18+
19+
# str1 = input("Enter String : ")
20+
str1 = "The quick brown fox jumps over the lazy dog."
21+
patterns = ["dog","lazy","fox"]
22+
23+
matching(str1,patterns)

21_Question.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 21. Write a Python program to find the substrings within a string.
2+
3+
import re
4+
5+
pattern = "exercises"
6+
str1 = 'Python exercises, PHP exercises, C# exercises'
7+
8+
9+
lst = re.findall(pattern, str1)
10+
print(f"Substrings : {lst}")

2_Question.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 2. Write a Python program that matches a string that has an a followed by zero or more b's.
2+
3+
import re
4+
5+
str1 = input("Enter string : ")
6+
7+
result = re.findall(r'^a(b*)$',str1)
8+
print(result)
9+

3_Question.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 3. Write a Python program that matches a string that has an a followed by one or more b's.
2+
3+
4+
5+
import re
6+
7+
str1 = input("Enter String : ")
8+
9+
res = re.findall(r'^a(b+)$',str1)
10+
11+
print(res)

4_Question.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 4. Write a Python program that matches a string that has an a followed by zero or one 'b'.
2+
3+
import re
4+
5+
str1 = input("Enter string : ")
6+
7+
res = re.findall(r'^a(b?)$',str1)
8+
9+
print(res)

5_Question.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 5. Write a Python program that matches a string that has an a followed by three 'b'.
2+
3+
import re
4+
5+
str1 = input("Enter string : ")
6+
7+
res = re.findall(r'^ab{3}$',str1)
8+
print(res)

6_Question.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 6. Write a Python program that matches a string that has an a followed by two to three 'b'.
2+
3+
import re
4+
5+
str1 = input("Enter String : ")
6+
7+
res = re.findall(r'^ab{2,3}$',str1)
8+
print(res)

7_Question.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 7. Write a Python program to find sequences of lowercase letters joined with a underscore.
2+
3+
import re
4+
5+
str1 = input("Enter String : ")
6+
7+
res = re.findall(r'[a-z]+_[a-z]+$',str1)
8+
print(res)

8_Question.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 8. Write a Python program to find the sequences of one upper case letter followed by lower case letters
2+
3+
4+
import re
5+
6+
str1 = input("Enter String : ")
7+
8+
res = re.findall(r'[A-Z][a-z]+$',str1)
9+
10+
print(res)
11+
12+
13+

9_Question.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 9. Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'.
2+
3+
import re
4+
5+
6+
str1 = input("Enter String : ")
7+
8+
res = re.findall(r'a.*b$',str1)
9+
print(res)

0 commit comments

Comments
 (0)