Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.4 String Operations
3.4 Team Teach String Operations
Sample Hack - String Analyzation
- Determine metrics about strings (length, chars, palidrome(?), etc.)
string = "Brawl Stars is a fun game!"
string2 = "Star Brawl war Brats"
print("String 1: Brawl Stars is a fun game!")
length = len(string)
print("Length:", length)
def count_vowels(input_string):
vowels = 'aeiouAEIOU'
count = 0
for char in input_string:
if char in vowels:
count += 1
return count
print("Vowel Count:", count_vowels(string))
def average_word_length(input_string):
words = input_string.split()
if not words:
return 0
total_length = sum(len(word) for word in words)
average_length = total_length / len(words)
return average_length
print("Average Word Count:", average_word_length(string))
def palindrome(input_string):
string = input_string.replace(" ","").lower()
return string == string[::-1]
print("Palindrome or Not?", palindrome(string))
print("String 2: Star Brawl war Brats")
length2 = len(string2)
print("Length:", length2)
print("Vowel Count:", count_vowels(string2))
print("Average Word Count:", average_word_length(string2))
print("Palindrome or Not?", palindrome(string2))
String 1: Brawl Stars is a fun game!
Length: 26
Vowel Count: 7
Average Word Count: 3.5
Palindrome or Not? False
String 2: Star Brawl war Brats
Length: 20
Vowel Count: 4
Average Word Count: 4.25
Palindrome or Not? True