String Project Solutions
Level 1: Name Combiner
Level 2: Word Play
Level 3: Sentence Formatter
Level 1: Name Combiner
Goal: Ask the user for their first and last name, and greet them.
Step 1: Ask the user their first and last names using the input() function, and save their answers as variables
Fname = input("What is your first name? ")
Lname = input("What is your last name? ")
Use the variables Fname and Lname to store the user's answer to each question.
Step 2: Combine their first and last names into a single variable
name = Fname + " " + Lname
The variable name contains the user's full name, using string concatenation to combine fname, a space, and lname.
Step 3: Greet the user
print("Hello, " + name + "!")
Inside the print function. concatenate the strings "Hello, ", name, and "!".
Level 2: Word Play
Goal: Ask the user a word, and use indexes/slicing to print first/last letter, the first half and second half of the word, and reverse of the word.
Step 1: Prompt the user for a word and save it as a variable
word = input("Type a word: ")
Input function is used to save the user's input as the variable word.
Step 2: Find the first and last letters using indexing
firstLetter = word[0]
lastLetter = word[-1]
print("First letter:", firstLetter)
print("Last letter:", lastLetter)
Index 0 is the first character, index -1 is the last character.
Step 3: Find the first and second halves using slicing
n = len(word)
half = n // 2
firstHalf = word[:half]
secondHalf = word[half:]
print("First half:", firstHalf)
print("Second half:", secondHalf)
The len function gives us the length of the word (how many letters there are), and it is saved in the variable n. n // 2 is saved in the variable half, which means to divide n by 2 and round down to the nearest whole number. word[:half] means slice from index 0 up to but not including half. word[half:] means start from half and go to the end. If the word has an odd length, the middle letter will be part of the second half.
Step 4: Reverse the word using slicing
reverseWord = word[::-1]
print("Reversed:", reverseWord)
[::-1] copies the string in reverse order, something that works on any string.
Level 3: Sentence Formatter
Goal: Ask the user for a sentence, use a variety of String methods and f strings to reformat the sentence
Step 1: Prompt the user for a word and save it as a variable
word = input("Type a word: ")
Input function is used to save the user's input as the variable word.
Step 2: Find the first and last letters using indexing
firstLetter = word[0]
lastLetter = word[-1]
print("First letter:", firstLetter)
print("Last letter:", lastLetter)
Index 0 is the first character, index -1 is the last character.
Step 3: Find the first and second halves using slicing
n = len(word)
half = n // 2
firstHalf = word[:half]
secondHalf = word[half:]
print("First half:", firstHalf)
print("Second half:", secondHalf)
The len function gives us the length of the word (how many letters there are), and it is saved in the variable n. n // 2 is saved in the variable half, which means to divide n by 2 and round down to the nearest whole number. word[:half] means slice from index 0 up to but not including half. word[half:] means start from half and go to the end. If the word has an odd length, the middle letter will be part of the second half.
Step 4: Reverse the word using slicing
reverseWord = word[::-1]
print("Reversed:", reverseWord)
[::-1] copies the string in reverse order, something that works on any string.