String Assignments
String is a Collection of __________
2. Strings are enclosed within _________
Single Quotes (' ')Double Quotes (" ")Triple Quotes (''' ''' or """ """)Any of the Above
3. Ask the user to enter their name and display it in reverse order (last name first).
Sample Input: Ayush Mishra Sample Output: Mishra Ayush
Solution
name=input('Enter your name:')
name=name.split(' ')
rev_name=name[-1] + name[0]
print(f'Reversed Name:{rev_name}')Take a word as input from the user and print the first and last character.
What is the Positive and negative index number of space in Word - David Joseph:
Solution
Positve Index number of first letter D starts with 0, hence index number of space will be: 5
Negative Index number of last letter h is -1, hence index number of space will be: -7
Code :
name = 'David Joseph'
pi = name.find(' ')
print('Positive index : ',pi)
total_char = len(name)
ni = pi - total_char
print('Negative index : ',ni)
How to get the first character in upper case from the name inputted by the User.For example :name : 'david Joseph'Output : D
Solution
First thing that we need to do , is to take input from user.
name = input('Enter Your Name : ')
Now name given by user can be in lower case or in upper case or a mix of it. But we need first character in uppercase, So what method do we use to convert string in Upper case : upper().
result = name[0]
It will give you first character , but we need to be sure that it is in uppercase. So it is better to assign this value in result.
result = name[0].upper()
print(f'First Character in String is {result}.')
You work in a company and you are given customers number, But many customers filled wrong data, so you need to check if the number given by customer is right or not.
Phone Number should match following Criteria :
Only numbers , spaces are allowed though (so few customers wrote their number with spaces.)10 character only.
For Example
input : 976 543 2319
Output :
check_number : True
ten_character : True
input : 976 543 a319
Output :
check_number : False
ten_character : True
Solution
First thing that we need to do is to take input from User.
number = input('Enter Customer Number : ')
Now as we know that spaces are allowed, So it is better to replace them with nothing , so that we don't have to worry about them.
number = number.replace(' ','')
Now we need to check out first criteria and that is to check if it is number.
check_number = number.isnumeric()
Now we need to check our second criteria and that is to check if there are only 10 digits.
ten_character = len(number)==10
Voila !!!
print(f'Check Number : {check_number}')
print(f'Ten character : {ten_character}')
Find the second maximum digit from a number.
Solution
First thing that we need to do is to take input from User.
number = input('Enter the number : ')
Now what we need to do is to find the maximum number.
first_max = max(number)
Remove this maximum number from the number, so that our second maximum number
number = number.replace(first_max,'')
Now that the maximum number is removed from the string, We can find our second maximum number
second_max = max(number)
print(second_max)
You work in a bank, You are tasked to create a pin with the help of their customer name and birthyear .
Input Customer Name : david josh
Input Date Of Birth : 09-12-1997
Output:
pin : David1997
Solution
First we will take inputs from user.
name = input('Enter Customer Name : ')
dob = input('Enter date of birth : ')
Now we need to split first name from the user name and convert it into title.
firstname = name.split(' ')[0].title()
Now we need to get year from User Date of Birth , we can achieve it by splitting last four digits.
year = dob[-4:]
Let's concatenate both to get Customer's pin number.
pin = firstname + year
print(pin)
You are tasked to swap first and last characters of a string.
For Example:
Input string : 'python'
Output :
'nythop'
Solution
First we will take input From user.
name = input('Enter the string : ')
We will get First and last character with the help of indexing.
f = name[0]
l = name[-1]
Now we need to slice middle string, It means from second character to second last character.
mid_str = name[1:-1]
Now you can concatenate in desired order.
print(l+mid_str+f)
Take a String from user consisting only alphabets and spaces, and count How many Consonants and Vowels are in the string.
input string : 'This is example'
Output :
Total Alphabets : 13
Total Vowels : 5
Total Consonants : 8
Check if a string is palindrome or not. Palindrome is a string that does not change even if you reverse it.
For example:
input string : 'rotator'
Output:
Palindrome : True
input string : 'python'
Output:
Palindrome : False
Solution
Let us take an input from user.
st = input('Enter the string : ')
Let us reverse the string.
new_st = st[::-1]
Now let's compare both strings.
print(st==new_st)
Imagine You run an Institute where all your students are stored in a variable something like this :
students = 'david mark bill sam'
You have a few tasks to do like :
students = 'david mark bill sam'
input :
Enter the name of the student : jeff
david mark bill sam jeff
students = 'david mark bill sam'
input :
Enter the name of the student : sam
david mark bill
students = 'david mark bill sam'
input :
Enter the name of the student to find : sam
True
students = 'david mark bill sam'
input :
Enter the name of the old student to replace : sam
Enter the name of the new student : karen
david mark bill karen
Solution
So we are given a string for reference; let us use this reference of students to perform such operations.
students = 'david mark bill sam'
student = input('Enter the name of the student : ')
students = students + student
student = input('Enter the name of the student : ')
students = students.replace(student,'')
student = input('Enter the name of the student : ')
check = student in students
print(check)
old_student = input('Enter the name of the student to replace : ')
new_student = input('Enter the name of the new student: ')
students = students.replace(old_student,new_student)
print(students)
Find out the name of the company from a URL inputted by the user. Example: url= www.facebook.com\ Output: facebook
Input an email address and extract the username part (before the @).
Take a string input, count the number of vowels and then remove all the vowels.
Ask the user for two strings and check if the second string is a substring of the first.
Which of the following is not a valid string method in Python?
a) upper() b) lower() c) title() d) subtract()
Which of the following code snippets will concatenate two strings in Python?
a) string1.append(string2) b) string1.join(string2) c) string1 + string2 d) string1.concat(string2)
Which of the following is the correct way to access the first character of a string in Python?
a) string[1] b) string[0] c) string[2] d) string[-1]
Which of the following code snippets will reverse a string in Python?
a) string.reverse() b) reversed(string) c) string[::-1] d) string.reverse_string()
Which of the following code snippets will return the length of a string in Python?
a) len(string) b) string.length() c) string.len() d) length(string)
Which of the following code snippets will replace all occurrences of a substring in a string in Python?
a) string.replace(old, new) b) string.sub(old, new) c) string.replace_substring(old, new) d) string.substitute(old, new)
Which of the following code snippets will split a string into a list of substrings based on a delimiter in Python?
a) string.split(delimiter) b) string.divide(delimiter) c) string.cut(delimiter) d) string.separate(delimiter)
Which of the following code snippets will convert a string to all lowercase letters in Python?
a) string.lowercase() b) string.to_lower() c) string.lower() d) string.convert_to_lower()
Which of the following code snippets will remove leading and trailing whitespace from a string in Python?
a) string.strip() b) string.trim() c) string.remove_whitespace() d) string.trim_whitespace()
Which of the following code snippets will check if a string starts with a specific substring in Python?
a) string.check_start(substring) b) string.begins_with(substring) c) string.startswith(substring) d) string.starts(substring)
Which of the following code snippets will split a string into a list of characters in Python?
a) string.split() b) string.chars() c) list(string) d) string.list()
Which of the following code snippets will check if a substring exists in a string in Python?
a) string.exists(substring) b) string.contains(substring) c) substring in string d) string.sub(substring)
Which of the following code snippets will count the number of occurrences of a substring in a string in Python?
a) string.count(substring) b) string.occurrences(substring) c) string.find(substring) d) string.index(substring)
Which of the following code snippets will check if all characters in a string are alphanumeric in Python?
a) string.is_alpha_num() b) string.is_alphanumeric() c) string.isalpha() and string.isdigit() d) string.isalnum()
Which of the following code snippets will convert a string to a list of words in Python?
a) string.split() b) string.words() c) list(string) d) string.list_words()
Which of the following code snippets will check if a string ends with a specific substring in Python?\
a) string.end(substring) b) string.check_end(substring) c) string.endswith(substring) d) string.ends(substring)
Which of the following code snippets will return the index of the first occurrence of a substring in a string in Python?
a) string.find(substring) b) string.index(substring) c) string.first(substring) d) string.search(substring) e) Both a and b
Which of the following code snippets will check if all characters in a string are in lowercase in Python?
a) string.islower() b) string.is_lowercase() c) string.islowercase() d) string.is_lower_case()
Which of the following code snippets will remove all occurrences of a character in a string in Python?
a) string.remove(char) b) string.delete(char) c) string.replace(char, '') d) string.sub(char, '')
Which of the following code snippets will check if all characters in a string are in uppercase in Python?
a) string.is_uppercase() b) string.isupper() c) string.is_upper() d) string.is_upper_case()
Last updated