PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  1. Python For Data Analytics
  2. 1.Python
  3. 2.Python Assignments

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

4. 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 : ')

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

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

input : 976 543 a319

Output :

check_number : False

ten_character : False

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

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

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

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

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

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

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

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

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

PreviousFunctions- Basic AssignmentsNextString CheatSheet

Last updated 2 months ago

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