Software Development Exam  >  Software Development Notes  >  Basics of Python  >  Assignment: Python Strings

Assignment: Python Strings | Basics of Python - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which of the following is NOT a valid method to concatenate two strings in Python?
(a) Using the + operator
(b) Using the join() method
(c) Using the concat() function
(d) Using formatted string literals

Ans. (c)

Q.2. Which of the following methods can be used to check if a string starts with a specific substring in Python?
(a) startswith()
(b) endswith()
(c) contains()
(d) find()

Ans. (a)

Q.3. What will be the output of the following code snippet?

string = "Hello, World!"

print(string[3:8])

(a) Hello
(b) lo, Wo
(c) lo, W
(d) lo, Wor

Ans. (c)

Q.4. Which of the following methods can be used to convert a string to uppercase in Python?
(a) to_upper()
(b) upper()
(c) uppercase()
(d) capitalize()

Ans. (b)

Q.5. What will be the output of the following code snippet?

string = "Hello, World!"

print(string.split(","))

(a) ['Hello', ' World!'] 
(b) ['Hello,', ' World!'] 
(c) ['Hello, World!'] 
(d) ['Hello', 'World!']

Ans. (a)

Higher Order Thinking Questions (HOTS)

Q.1. Write a Python program that takes a string as input and counts the number of uppercase letters in it.

def count_uppercase(string):

    count = 0

    for char in string:

        if char.isupper():

            count += 1

    return count

Q.2. Write a Python function that checks if a given string is a palindrome or not. A palindrome is a string that reads the same forwards and backwards, ignoring spaces, punctuation, and capitalization.

def is_palindrome(string):

    cleaned_string = ''.join(char.lower() for char in string if char.isalpha())

    return cleaned_string == cleaned_string[::-1]

Q.3. Write a Python program that takes a sentence as input and returns the words in reverse order. For example, if the input sentence is "Hello, how are you?", the program should return "you? are how Hello,".

def reverse_words(sentence):

    words = sentence.split()

    return ' '.join(words[::-1])

Q.4. Write a Python program to remove all the vowels from a given string.

def remove_vowels(string):

    vowels = 'aeiouAEIOU'

    return ''.join(char for char in string if char not in vowels)

Q.5. Write a Python program to find the longest word in a given string.

def longest_word(string):

    words = string.split()

    return max(words, key=len)

Fill in the Blanks

Q.1. The method __________ returns the number of occurrences of a substring in a string.

Ans. count()

Q.2. The __________ method is used to remove leading and trailing whitespace from a string.

Ans. strip()

Q.3. The method __________ returns a new string in which all occurrences of a substring in the original string are replaced with another substring.

Ans. replace()

Q.4. The __________ method is used to convert a string to a list of substrings, split by a specified delimiter.

Ans. split()

Q.5. The __________ method can be used to check if a given string contains only numeric characters.

Ans. isdigit()

True/False

Q.1. In Python, strings are immutable, meaning they cannot be changed once created.

Ans. True

Q.2. The strip() method removes all occurrences of a specified character from a string.

Ans. False

Q.3. The find() method returns the index of the first occurrence of a specified substring in a string.

Ans. True

Q.4. The isnumeric() method returns True if all characters in a string are numeric.

Ans. False

Q.5. Python uses zero-based indexing for strings, where the first character is at index 1.

Ans. False

Hands-On Questions

Q.1. Write a Python program that takes a string as input and prints the string in reverse order.

def reverse_string(string):

    return string[::-1]

Q.2. Write a Python program that takes two strings as input and checks if they are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of another word or phrase.

def is_anagram(string1, string2):

    return sorted(string1.lower()) == sorted(string2.lower())

Q.3. Write a Python program that takes a sentence as input and counts the number of words in it.

def count_words(sentence):

    words = sentence.split()

    return len(words)

Q.4. Write a Python program that takes a string as input and prints the frequency of each character in the string.

def char_frequency(string):

    frequency = {}

    for char in string:

        frequency[char] = frequency.get(char, 0) + 1

    return frequency

Q.5. Write a Python program that takes a string as input and checks if it is a valid email address. Assume a valid email address contains a username, followed by the @ symbol, and then a domain name.

import re


def is_valid_email(email):

    pattern = r'^[\w\.-]+@\w+\.\w+$'

    return bool(re.match(pattern, email))

The document Assignment: Python Strings | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development
49 videos|38 docs|18 tests

Top Courses for Software Development

49 videos|38 docs|18 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Assignment: Python Strings | Basics of Python - Software Development

,

practice quizzes

,

Previous Year Questions with Solutions

,

Semester Notes

,

Viva Questions

,

Assignment: Python Strings | Basics of Python - Software Development

,

ppt

,

past year papers

,

Free

,

video lectures

,

MCQs

,

pdf

,

mock tests for examination

,

shortcuts and tricks

,

Sample Paper

,

study material

,

Important questions

,

Extra Questions

,

Objective type Questions

,

Summary

,

Exam

,

Assignment: Python Strings | Basics of Python - Software Development

;