String Assignments

  1. String is a Collection of __________

Solution

Characters

2. Strings are enclosed within _________

  • Single Quotes (' ')

  • Double Quotes (" ")

  • Triple Quotes (''' ''' or """ """)

  • Any of the Above

Solution

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}')
  1. Take a word as input from the user and print the first and last character.

Solution
word=input('Enter a word:')
print(f'First character:{word[0]')
print(f'Last character:{word[-1]')
  1. 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)

  1. 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}.')

  1. 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 :

  1. Only numbers , spaces are allowed though (so few customers wrote their number with spaces.)

  2. 10 character only.

For Example

input : 976 543 2319

Output :

check_number : True

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}')

  1. 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)

  1. 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)

  1. 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)

  1. 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

Solution
#Take a string input from user.
st = input('Enter the string : ').lower()
 
#Now that we do not need spaces , let's remove them for good, by replacing them with empty string.
st = st.replace(' ','') 
#Now only alphabets are in the string , so let us calculate number of alphabets by using len() function.
total_alpha = len(st) 
#Now to count all vowels , we will use count method.
total_vowel = st.count('a')+st.count('e')+st.count('i')+st.count('o')+st.count('u') 
#To count Consonants, all we need to do is to subtract total vowel from total alphabets.
total_cons = total_alpha-total_vowel 
#Voilla !!!
print(total_alpha) 
print(total_vowel) 
print(total_cons)
  1. 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)

  1. 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

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)

  1. Find out the name of the company from a URL inputted by the user. Example: url= www.facebook.com Output: facebook

Solution
url=input('Enter a url')
url=url.split('.')
company_name=url[1]
print(f'The name of the company is:{company}'
  1. Input an email address and extract the username part (before the @).

Solution
email=input('Enter Email ID:')
username= email[0:email.find('@')]
print(f'Your Username:{username}')
  1. Take a string input, count the number of vowels and then remove all the vowels.

Solution
str1=input('Enter a string:').lower()
total_vowels= str1.count('a') + str1.count('e') + str1.count('i') + str1.count('o') + str1.count('u')
str1=str1.replace('a','').replace('e','').replace('i','').replace('o','').replace('u','')
  1. Ask the user for two strings and check if the second string is a substring of the first.

Solution
str1=input('Enter a sting:')
substr=input('Enter a substring:')

result= substr in str1
print(f'Substring:{result}')

Which of the following is not a valid string method in Python?

a) upper() b) lower() c) title() d) subtract()

Solution

Answer: 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)

Solution

Answer: c) string1 + 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]

Solution

Answer: b) string[0]

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()

Solution

Answer: c) string[::-1]

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)

Solution

Answer: a) len(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)

Solution

Answer: a) string.replace(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)

Solution

Answer: a) string.split(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()

Solution

Answer: c) string.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()

Solution

Answer: a) string.strip()

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)

Solution

Answer: c) string.startswith(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()

Solution

Answer: c) list(string)

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)

Solution

Answer: c) substring in string

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)

Solution

Answer: a) string.count(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()

Solution

Answer: 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()

Solution

Answer: a) string.split()

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)

Solution

Answer: c) string.endswith(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

Solution

Answer: option e

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()

Solution

Answer: a) string.islower()

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, '')

Solution

Answer: c) string.replace(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()

Solution

Answer: b) string.isupper()

Last updated