String
String
Write a function reverse_string(s) that takes a string s and returns the string in reverse order.
python
Copy code
def reverse_string(s):
Example:
python
Copy code
Create a function count_vowels(s) that takes a string s and returns the number of vowels (a, e, i, o, u)
in the string.
python
Copy code
def count_vowels(s):
Example:
python
Copy code
Write a function is_palindrome(s) that checks if the string s is a palindrome (reads the same
backward as forward). The function should return True or False.
python
Copy code
def is_palindrome(s):
Example:
python
Copy code
python
Copy code
def compress_string(s):
Example:
python
Copy code
Write a function remove_duplicates(s) that removes duplicate characters from the string s while
maintaining the original order of characters.
python
Copy code
def remove_duplicates(s):
Example:
python
Copy code
4o mini
You said:
ChatGPT
Sure! Here are some additional Python questions based on string manipulation:
Write a function char_frequency(s) that takes a string s and returns a dictionary where the keys are
characters and the values are the number of times each character appears in the string.
python
Copy code
def char_frequency(s):
Example:
python
Copy code
Create a function capitalize_words(s) that takes a string s and returns the string with the first letter of
each word capitalized.
python
Copy code
def capitalize_words(s):
Example:
python
Copy code
Write a function longest_word(s) that takes a string s and returns the longest word in the string. If
there are multiple words of the same length, return the first one.
python
Copy code
def longest_word(s):
Example:
python
Copy code
Create a function are_anagrams(s1, s2) that checks if two strings s1 and s2 are anagrams of each
other (i.e., they contain the same characters in a different order).
python
Copy code
Example:
python
Copy code
Write a function find_substring(s, substring) that returns the starting index of the first occurrence of
substring in s. If substring is not found, return -1.