Python Strings Practice
"The difference between the almost-right word & the right word is really a large matter—it’s the difference between the lightning bug and the lightning."
— Mark Twain
Easy
- Write a program that asks the user to input their name and then prints a welcome message using their name. Example:
Welcome, John!
- Write a Python program that asks the user for their favorite color and prints how many letters the color has. Example:
Your favorite color has 4 letters.
- Write a program that takes a word from the user and prints the first and last letter of the word. Example: For the word "apple," the output should be:
First letter: a, Last letter: e
- Write a program to input a name and then print the number of letters in that name.
- Write a program to input a word and then print the first 3 and the last 2 characters of that word. In case the word input is less than 3 letters, return the input word itself.
- Write a program to input a sentence and then print the characters present at the odd index only.
- Ask the user to enter their favorite fruit, then print the fruit in all uppercase letters. Example:
MANGO
- Write a Python program to ask the user for two words and join them together with a space in between. Example:
hello world
- Ask the user for a word and then print that word repeated 3 times on the same line. Example: If the input is "fun," the output will be:
funfunfun
- Write a program to input a word and then print the the first half of that word.
- Write a Python program that asks the user to input a word and checks if the letter 'a' is in the word. If it is, print
'a' is in the word!
- Ask the user to input a word, then print the word backward. Example: For the input "python," the output will be:
nohtyp
- Write a Python function to check if a given string is a palindrome (reads the same forwards and backwards).
- Write a Python program to count how many vowels are present in a given string.
- Please enter a 10-digit phone number to verify if it's a valid Indian phone number. A valid Indian phone number starts with 6, 7, 8, or 9 and is exactly 10 digits long (e.g.,
9876543210, 9123456789
) - Write a Python program to find the length of a string without using the built-in
len()
function. - Write a Python function that takes a string as input, removes all spaces from the string, and returns the resulting string.
- Write a program that reads a number from the user and remove all zeroes from it and display the new number.
- Write a Python function to check if a given string contains only digits.
- Write a Python function to check if a given string contains only digits.(Note: Please dont use any inbuilt String function in this question)
- Write a program to ask the user for their full name and then print only their initials. Example: For "John Doe," the output should be:
J.D.
- Write a Python program to replace all occurrences of a specific character in a string with another character.
- Write a Python program to capitalize the first letter of each word in a given string.
- Write a Python function that concatenates two strings without using the
+
operator. - Write a Python program to check if a string starts with a given substring.
- Write a Python function that counts the number of uppercase and lowercase characters in a string.
- Write a Python program to find all occurrences of a substring in a given string.
- Write a program to accept and change the case of each letter of the string. Display the new string.
- Write a program that accepts a word and checks if it is Palindrome or just a special word.
Hint APalindrome
is a word which reads that same backwards as forward. For Example: MADAM,MALAYALAM,CIVIC etc .
Special Words
are those which start and end with the same character. For example, COMIC, WINDOW ,etc . - Write a Python function that counts the number of words in a given sentence.
- Write a program to accept a word and convert into lowercase if it is in uppercase, and display the new word by replacing only the vowels with the character following it.
- Write a Python program that takes a phone number as input from the user, masks all digits except the first two and last two, and then prints the masked phone number.
- Write a Python program to input a word and print the new word formed by arranging each letter lexicographically (alphabetical order).
- Write a Python program to find the character that appears most frequently in a string.
- Write a Python program to input a sentence from the user and print the first word of that word
- Write a Python function that removes all duplicate characters from a string and returns the result.
- Write a Python function that takes two strings as input and checks if one string is a substring of the other.
- Write a Python function to reverse each word in a given sentence, while keeping the words in the same order.
- Write a Python program to remove all non-alphabetic characters from a string.
- Write a program that reads several lines of text and prints a table indicating the number of occurrences of each letter of the alphabet in the text.
- Write a Python function to split a string by spaces and return a list of words.
- Write a Python program to input a sentence from the user and print the last word of that word
- Write a Python program to convert a string to a list of characters and print it.
- Write a program that reads a series of strings and prints only those that end with the letters "ed"
- Write a program that reads a date in the format
07/21/2003
and prints it in the formatJuly 21, 2003
.
Moderate
Advanced
For example, the phrase
To be, or not to be: that is the question:
contains one “a,” two “b’s,” no “c’s,” and so on.
Dates are commonly printed in several different formats in business correspondence. Two of the more common formats are
07/21/2003
and July 21, 2003
.