KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

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

  1. Create a string with the value "Artificial Intelligence".
  2. Convert the entire string to uppercase and print the result.
  3. Find and print the position of the substring "Intelligence" within the string.
  4. Replace the substring "Artificial" with "Machine" and print the new string.
  5. Check if the string starts with "Machine" and print the result (True/False).
  6. Count and print the number of occurrences of the letter 'i' in the string.

Python String Manipulation

1 Like

Answer

original_string = "Artificial Intelligence"

uppercase_string = original_string.upper()
print("Uppercase string:", uppercase_string)

position = original_string.find("Intelligence")
print("Position of 'Intelligence':", position)

new_string = original_string.replace("Artificial", "Machine")
print("New string:", new_string)

starts_with_machine = new_string.startswith("Machine")
print("Starts with 'Machine':", starts_with_machine)

count_i = original_string.count('i')
print("Number of occurrences of 'i':", count_i)

Output

Uppercase string: ARTIFICIAL INTELLIGENCE
Position of 'Intelligence': 11
New string: Machine Intelligence
Starts with 'Machine': True
Number of occurrences of 'i': 4

Answered By

3 Likes


Related Questions