Python String Practice Questions
Python String Practice Questions
Use the swapcase() method in Python to convert each lowercase letter to uppercase and vice versa. For the string "PyThOn", the result would be "pYtHoN" .
By swapping the first with the second character, the third with the fourth, and the fifth with the sixth, the transformed word "abcdef" becomes "badcfe" .
The domain name can be extracted by splitting the email address at the '@' symbol and selecting the second element, which is "company.com" .
First, use the split() method to separate words in the sentence, resulting in a list. Apply len() to this list to count the number of words, which is 6 for "Python is a powerful programming language." .
Use the startswith('www') method to check if a string starts with 'www' and the endswith('.com') method to check if it ends with '.com'. Both conditions should be true for the statement to return True .
The output would be "Final_Project_Report.docx" as the spaces are replaced with underscores .
To swap the first and last characters using slicing, you can reassemble the string as string[-1] + string[1:-1] + string[0]. For "coding", this produces "godinc" .
In Python, you can access the first character using string[0] and the last character using string[-1]. For "example", the first character is 'e' and the last character is 'e' .
You can determine the remaining characters by subtracting the number of characters used in the bio from 150. For instance, if the bio is "Coding | Traveler | Foodie" with 26 characters, the remaining characters would be 150 - 26 = 124 .
To count occurrences of '#Python' in a text, use the count() method, which will count how many times '#Python' appears in the text. In the text "#Python is amazing. #AI and #Python will rule the world.", '#Python' appears 2 times .