Python String Manipulation Exercises
Python String Manipulation Exercises
Split the string into a list, use a set to remove duplicates, and rejoin words into a string. 'Python Exercises Practice Solution Exercises' becomes 'Python Exercises Practice Solution'.
To remove characters at odd indices, iterate over the string and add characters at even indices to a new string. For example, 'abcdef' would become 'ace'.
Locate the indices of 'not' and 'poor'. If 'not' precedes 'poor', replace the substring from 'not' to 'poor' with 'good'. Example: 'The lyrics is not that poor!' becomes 'The lyrics is good!'. If 'poor' precedes 'not', no change is made.
To swap the first two characters of two strings, extract these substrings, swap them, and concatenate with the rest of the respective strings, separated by a space. 'abc' and 'xyz' become 'xyc abz'.
Check if the string length is at least three, and if it ends with 'ing'. If true, add 'ly' to the string; otherwise, append 'ing'. For example, 'abc' becomes 'abcing' and 'string' becomes 'stringly'.
The function iterates through the list, keeping track of the longest word and updating it whenever a longer word is found. The function returns the longest word and its length, like 'Exercises' with length 9.
Split the string by commas to create a list, remove duplicates using a set, and sort it. For example, 'red, white, black, red, green, black' would result in ['black', 'green', 'red', 'white']
To replace occurrences of the first character with '$', iterate through the string starting from the second character, comparing each character to the first. Replace matches with '$' and reconstruct the string. For 'restart', this results in 'resta$t'.
To write a Python program to count the frequency of characters in a string like 'google.com', iterate through each character in the string, use a dictionary to keep count of each character's occurrences. For 'google.com', the output should be: g is occurring 2 times, o is occurring 3 times, etc.
You can define a function that checks if the length of the input string is less than 2 and return an empty string. Otherwise, concatenate the first two and last two characters of the string. For example, 'w3resource' becomes 'w3ce'.