w3resource logo
Python Exercises

Python Regular Expression - Exercises, Practice, Solution

Python Regular Expression [50 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

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). Go to the editor

Click me to see the solution

2. Write a Python program that matches a string that has an a followed by zero or more b's. Go to the editor

Click me to see the solution

3. Write a Python program that matches a string that has an a followed by one or more b's. Go to the editor

Click me to see the solution

4. Write a Python program that matches a string that has an a followed by zero or one 'b'. Go to the editor

Click me to see the solution

5. Write a Python program that matches a string that has an a followed by three 'b'. Go to the editor

Click me to see the solution

6. Write a Python program that matches a string that has an a followed by two to three 'b'. Go to the editor

Click me to see the solution

7. Write a Python program to find sequences of lowercase letters joined with a underscore. Go to the editor

Click me to see the solution

8. Write a Python program to find sequences of one upper case letter followed by lower case letters. Go to the editor

Click me to see the solution

9. Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'. Go to the editor

Click me to see the solution

10. Write a Python program that matches a word at the beginning of a string. Go to the editor

Click me to see the solution

11. Write a Python program that matches a word at end of string, with optional punctuation. Go to the editor

Click me to see the solution

12. Write a Python program that matches a word containing 'z'. Go to the editor

Click me to see the solution

13. Write a Python program that matches a word containing 'z', not start or end of the word. Go to the editor

Click me to see the solution

14. Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores. Go to the editor

Click me to see the solution

15. Write a Python program where a string will start with a specific number. Go to the editor

Click me to see the solution

16. Write a Python program to remove leading zeros from an IP address. Go to the editor

Click me to see the solution

17. Write a Python program to check for a number at the end of a string. Go to the editor

Click me to see the solution

18. Write a Python program to search the numbers (0-9) of length between 1 to 3 in a given string. Go to the editor

"Exercises number 1, 12, 13, and 345 are important"

Click me to see the solution

19. Write a Python program to search some literals strings in a string. Go to the editor
Sample text : 'The quick brown fox jumps over the lazy dog.'
Searched words : 'fox', 'dog', 'horse'

Click me to see the solution

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. Go to the editor

Sample text : 'The quick brown fox jumps over the lazy dog.'
Searched words : 'fox'

Click me to see the solution

21. Write a Python program to find the substrings within a string. Go to the editor

Sample text :

'Python exercises, PHP exercises, C# exercises'

Pattern :

'exercises'

Note: There are two instances of exercises in the input string.

Click me to see the solution

22. Write a Python program to find the occurrence and position of the substrings within a string. Go to the editor

Click me to see the solution

23. Write a Python program to replace whitespaces with an underscore and vice versa. Go to the editor

Click me to see the solution

24. Write a Python program to extract year, month and date from a an url. Go to the editor

Click me to see the solution

25. Write a Python program to convert a date of yyyy-mm-dd format to dd-mm-yyyy format. Go to the editor

Click me to see the solution

26. Write a Python program to match if two words from a list of words starting with letter 'P'. Go to the editor

Click me to see the solution

27. Write a Python program to separate and print the numbers of a given string. Go to the editor

Click me to see the solution

28. Write a Python program to find all words starting with 'a' or 'e' in a given string. Go to the editor

Click me to see the solution

29. Write a Python program to separate and print the numbers and their position of a given string. Go to the editor

Click me to see the solution

30. Write a Python program to abbreviate 'Road' as 'Rd.' in a given string. Go to the editor

Click me to see the solution

31. Write a Python program to replace all occurrences of space, comma, or dot with a colon. Go to the editor

Click me to see the solution

32. Write a Python program to replace maximum 2 occurrences of space, comma, or dot with a colon. Go to the editor

Click me to see the solution

33. Write a Python program to find all five characters long word in a string. Go to the editor

Click me to see the solution

34. Write a Python program to find all three, four, five characters long words in a string. Go to the editor

Click me to see the solution

35. Write a Python program to find all words which are at least 4 characters long in a string. Go to the editor

Click me to see the solution

36. Write a python program to convert camel case string to snake case string. Go to the editor

Click me to see the solution

37. Write a python program to convert snake case string to camel case string. Go to the editor

Click me to see the solution

38. Write a Python program to extract values between quotation marks of a string. Go to the editor

Click me to see the solution

39. Write a Python program to remove multiple spaces in a string. Go to the editor

Click me to see the solution

40. Write a Python program to remove all whitespaces from a string. Go to the editor

Click me to see the solution

41. Write a Python program to remove everything except alphanumeric characters from a string. Go to the editor

Click me to see the solution

42. Write a Python program to find urls in a string. Go to the editor

Click me to see the solution

43. Write a Python program to split a string at uppercase letters. Go to the editor

Click me to see the solution

44. Write a Python program to do a case-insensitive string replacement. Go to the editor

Click me to see the solution

45. Write a Python program to remove the ANSI escape sequences from a string. Go to the editor

Click me to see the solution

46. Write a Python program to find all adverbs and their positions in a given sentence. Go to the editor

Sample text : "Clearly, he has no excuse for such behavior."

Click me to see the solution

47. Write a Python program to split a string with multiple delimiters. Go to the editor

Note : A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values.

Click me to see the solution

48. Write a Python program to check a decimal with a precision of 2. Go to the editor

Click me to see the solution

49. Write a Python program to remove words from a string of length between 1 and a given number. Go to the editor

Click me to see the solution

50. Write a Python program to remove the parenthesis area in a string. Go to the editor
Sample data : ["example (.com)", "w3resource", "github (.com)", "stackoverflow (.com)"]
Expected Output:
example
w3resource
github
stackoverflow
Click me to see the solution

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.