CompSci
  • Home
  •  Unit 1
    Output Exercises
     Unit 2
    Variables Math Modules Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    Iteration / Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Lists Exercises
     Unit 7
    Functions Exercises
  •  Unit 1
    Java Introduction Java Syntax Errors & Coding Conventions Format Method Exercises
     Unit 2
    Variable Introduction Math / Arithmetic Operators Obtaining a Value Formatting Numeric Output Concatenation & Constants Division & Type Casting Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    while Loops for Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Methods Method Parameters Method Return Types Exercises
  • Muddy City
  • Github Classroom
  •  Assignments
    Add an Assignment Edit Assignment Search Program Code
     Forum
    Ask a Question Search the Forum
     New Account
    New Account


Account Mgmt
Reset Password

Python - Strings


Exercises

Short + Long + Short

Difficulty: Low

Title: Short + Long + Short

Description:
Write a program that prompts a user for two strings. Determine the length of the two strings and output a string in the following order: short + long + short

  • Verify that the strings are not the same length. If they are the same length then re-prompt the user for two new words.
  • If either of the strings are empty, then re-prompt the user for new words.

Output:
Input a string: Hello
Input a string: Bye

ByeHelloBye


Input a string: Hello
Input a string: cooks
Input a string: Hello
Input a string: Bye

ByeHelloBye


Lowercase and Uppercase

Difficulty: Low

Title: Lowercase and Uppercase

Description:
Based upon user input of a sentence, replace lowercase characters with uppercase and vice versa.

  • If the user does not enter a multi-word sentence, re-prompt the user for a new sentence.

Output:
Input a string: This Is A Sample Sentence
New sentence: tHIS iS a sAMPLE sENTENCE


Input a string: This
Input a string: NEw SENTence
New sentence: neW sentENCE


First & Last Character

Difficulty: Low

Title: First & Last Character

Description:
Write a program to prompt the user for a word. Take the inputted word / string and if the word is 5 or more characters only output the first 3 and last 2 characters of a given string. If the string length is less than 5, print the original string instead.

  • Prompt the user for a word.

Output:
Input a string: abcdefg
abcfg


Input a string: abcd
abcd


Type of Characters

Difficulty: Medium

Title: Type of Characters

Description:
NOTE: Do not use lists / arrays to solve this. Just use String methods, loops, and conditionals.

Prompt the user for a String and count the total number of alphabets, digits and special characters in a string.

  • Alpha characters are a - z or A - Z
  • Digits are 0 - 9
  • Special Characters are !, @, #, $, %, ^, &, *, .
  • Ignore spaces

Sample Code - isalpha() and isdigit() Methods
txt = "Hello"
txt.isalpha()

nbr = 12345
nbr.isdigit()

Output:
Input the string : W3lc0me to p3ri0d 3 Pr0gramming Fundam3ntals!!

Number of alpha characters: 32
Number of digits: 7
Number of special characters: 2


Longest and Shortest Word

Difficulty: Medium

Title: Longest and Shortest Word

Description:
Write a program that prompts a user for a sentence. Find the largest and smallest words in the sentence.

  • Re-prompt the user if they did not enter two or more words.
  • If two words are the same length output the first word from left to right.
  • Spaces and special characters do NOT count as the smallest characters.

Output:
Input a string: It is a string with smallest and largest word

Largest word: smallest
Smallest word: a


Input a string: It
Input a string: the
Input a string: It's storming outside

Largest word: storming
Smallest word: It's


Replacing Characters

Difficulty: Medium

Title: Replacing Characters

Description:
Write a program to replace specified characters with another character.

  • First prompt the user for a 5 letter or more word.
  • Have error handling to make the user enter a 5 letter or more word. If they do not re-prompt the user till they enter a word with the correct number of letters.
  • Second prompt the user for a character(s) to replace.
  • Third prompt the user for a character to replace with.
  • If the character(s) are found replace the characters and output the new word. If the characters are not found, output "Characters not found".

Output:
Input a string: hello
Input character to replace: e
Input character to replace with: $

h$llo


Input a string: hello
Input character to replace: l
Input character to replace with: $

he$$o
Input a string: hello
Input character to replace: d
Input character to replace with: $

Character not found
Input a string: cat
Input a string: dog
Input a string: runner
Input character to replace: n
Input character to replace with: $

ru$$er


String Menu Option

Difficulty: High

Title: String Menu Option

Description:
NOTE: Do not use lists / arrays to solve this. Just use String methods, loops, and conditionals.

Create a program that has a menu option that solves simple String programs. The menu option input must be an integer and if the user does not enter one of the following options re-prompt the user till they enter one of the following menu options.

  • Option 1: Write a program that prompts a user for a single word String and removes characters that have odd index values. Output the new word.
  • Option 2: Write a program that prompts a user for a single word String and reverses the String.
  • Option 3: Write a program that prompts a user for a single word String. Convert the given string to all uppercase if it contains at least 2 uppercase characters in the first 4 characters, otherwise output the original word.
  • Option 4: Write a program that prompts a user for a single word String. Sort the string alphabetically (A - Z) based upon the characters in the String.
  • Option 5: Write a program that prompts a user for at least an even number of words Swap every other words order. For example "the sunflowers swayed gently" would output as "sunflowers the gently swayed". Re-prompt the user till they enter an even number of words.

Output:
Choose from the following menu options
1 - Remove odd characters
2 - Reverse the String
3 - Uppercase a String
4 - Sort alphabetically
5 - Word swap

Choose an option: 1
Input a string: hello

hlo


Choose an option: 2
Input a string: hello

olleh
Choose an option: 3
Input a string: HeLlo

HELLO

Choose an option: 3
Input a string: hello
hello

Choose an option: 4
Input a string: hello
ehllo

Choose an option: 5
Input an even number of words: Hello World Its Me
World Hello Me Its


How Many Times

Difficulty: High

Title: How Many Times

Description:
NOTE: Do not use lists / arrays to solve this. Just use String methods, loops, and conditionals

NOTE: The count() function will find every occurrence of a word, even if it part of another word (ex. he is found in the, shell, etc.). Instead to pass all the tests students will need to look at each individual word.

Prompt the user for a sentence and a word to find. Output the number of times that the word to find appears in the sentence as its own word.

  • Ignore case sensitivity (i.e. "H" and "h" should be treated as the same character).
  • If the word is not in the sentence then output: "Word not found"

Output:
Input a string: I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn’t really happy.
Word to find: happy

Count: 4


Input a string: I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn’t really happy.
Word to find: day

Word not found



Computer Science