Robotics & Artificial Intelligence

Write a Python program that performs the following operations on a string:

  1. Define a string with the value "Machine Learning".
  2. Convert the string to lowercase and print it.
  3. Replace the word "Machine" with "Deep".
  4. Check if the string ends with "Learning" and print the result (True/False).
  5. Count and print the number of occurrences of the letter "n".

Python String Manipulation

1 Like

Answer

s = "Machine Learning"

lower_s = s.lower()
print("Lowercase string:", lower_s)

replaced_s = s.replace("Machine", "Deep")
print("After replacement:", replaced_s)

ends = s.endswith("Learning")
print("Ends with 'Learning':", ends)

count_n = s.count('n')
print("Number of occurrences of 'n':", count_n)

Output

Lowercase string: machine learning
After replacement: Deep Learning
Ends with 'Learning': True
Number of occurrences of 'n': 3

Answered By

3 Likes


Related Questions