Advertisement
brandblox

python lab (16/04/2024)

Apr 16th, 2024 (edited)
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. #Q1. python program to check if a string starts with the and end with spain use regex
  2.  
  3.  
  4. import re
  5.  
  6. def check_string(input_string):
  7.     pattern = r'^the.*spain$'
  8.     if re.match(pattern, input_string, re.IGNORECASE):
  9.         return True
  10.     else:
  11.         return False
  12.  
  13. test_string1 = input("Enter string: ")
  14.  
  15. print(check_string(test_string1))
  16.  
  17. #output:
  18. Enter string: the spain
  19. True
  20. Enter string: the spain not
  21. False
  22.  
  23.  
  24. #Q. program to find all lowercase charcters aplhabetically between a and m, use regex
  25. import re
  26.  
  27. def find_lower_chars(input_string):
  28.     pattern = r'[a-m]'
  29.     result = re.findall(pattern, input_string)
  30.     return result
  31.  
  32. user_input = input("Enter a string: ")
  33. lowercase_chars = find_lower_chars(user_input)
  34. print("Lowercase characters between 'a' and 'm':", lowercase_chars)
  35.  
  36. #output:
  37. Enter a string: The quick brown fox jumps over the lazy dog
  38. Lowercase characters between 'a' and 'm': ['h', 'e', 'i', 'c', 'k', 'b', 'f', 'j', 'm', 'e', 'h', 'e', 'l', 'a', 'd', 'g']
  39.  
  40. #Q. program to search for the first white space character in the string
  41.  
  42.  
  43. import re
  44.  
  45. txt = "The rain in Spain"
  46. x = re.search("\s", txt)
  47.  
  48. print("The first white-space character is located in position:", x.start())
  49.  
  50. #Output:
  51. The first white-space character is located in position: 3
  52.  
  53. #Q. program to split at each whitespace character using regex
  54.  
  55. import re
  56.  
  57. def split_at_whitespace(input_string):
  58.     return re.split(r'\s+', input_string)
  59.  
  60. user_input = input("Enter a string: ")
  61. result = split_at_whitespace(user_input)
  62. print("Split at each whitespace character:", result)
  63.  
  64.  
  65. #Output:
  66. Enter a string: This is a demo input.
  67. Split at each whitespace character: ['This', 'is', 'a', 'demo', 'input.']
  68.  
  69.  
  70. #Q. replace every white space character with the number nine
  71.  
  72. import re
  73.  
  74. def replace_whitespace_with_nine(input_string):
  75.     return re.sub(r'\s', '9', input_string)
  76.  
  77. user_input = input("Enter a string: ")
  78. result = replace_whitespace_with_nine(user_input)
  79. print("String with every whitespace character replaced with '9':", result)
  80.  
  81.  
  82. #Output:
  83. Enter a string: This is a demo input
  84. String with every whitespace character replaced with '9': This9is9a9demo9inputp
  85.  
  86.  
  87. #Q: check if email valid or not:
  88. import re
  89.  
  90. def validate_email(email):
  91.     pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
  92.     regex = re.compile(pattern)
  93.     return bool(regex.match(email))
  94.  
  95. user_email = input("Enter your email address: ")
  96.  
  97. if validate_email(user_email):
  98.     print("Valid email address!")
  99. else:
  100.     print("Invalid email address. Please enter a valid email.")
  101.  
  102.  
  103. #Output:
  104. Enter your email address: arijit@gmail.com
  105. Valid email address!
  106.  
  107. Enter your email address: arijit@gmail
  108. Invalid email address. Please enter a valid email.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement