Skip to the content.
Home Accounts Setup Verify Play Hacks

Gaheera Babbar P4 Popcorn Hacks 3.4 Strings

3.4 Lesson Popcorn and Homework Hacks

Python

Gaheera Babbar P4 Popcorn Hacks 3.4 Strings

# Python Popcorn Hack 1: Get the lyrics of a clean song to find how many times the title of the song appears in the lyrics, find the index of the 50th word, replace the first verse with the last verse, concat two verses together to make your own song
# Step 1: Lyrics of song
lyrics = """
I, I just woke up from a dream
Where you and I had to say goodbye
And I don't know what it all means
But since I survived, I realized

Wherever you go, that's where I'll follow
Nobody's promised tomorrow
So I'ma love you every night like it's the last night
Like it's the last night

If the world was ending
I'd wanna be next to you
If the party was over
And our time on Earth was through
I'd wanna hold you just for a while
And die with a smile
If the world was ending
I'd wanna be next to you

(Ooh, ooh)

Ooh, lost, lost in the words that we scream
I don't even wanna do this anymore
'Cause you already know what you mean to me
And our love's the only war worth fighting for

Wherever you go, that's where I'll follow
Nobody's promised tomorrow
So I'ma love you every night like it's the last night
Like it's the last night

If the world was ending
I'd wanna be next to you
If the party was over
And our time on Earth was through
I'd wanna hold you just for a while
And die with a smile
If the world was ending
I'd wanna be next to you

Right next to you
Next to you
Right next to you
Oh-oh

If the world was ending
I'd wanna be next to you
If the party was over
And our time on Earth was through
I'd wanna hold you just for a while
And die with a smile
If the world was ending
I'd wanna be next to you
If the world was ending
I'd wanna be next to you
"""
# The song title
song_title = "die with a smile"

# Step 2: Find how many times the title appears in the lyrics
title_count = lyrics.lower().count(song_title.lower())
print(f'The title "{song_title}" appears {title_count} times in the song.')

# Step 3: Find the index of the 50th word
words = lyrics.split()  # Split lyrics into words
if len(words) >= 50:
    word_50th = words[49]  # Words are zero-indexed, so 50th word is at index 49
    index_of_50th = lyrics.index(word_50th)
    print(f'The index of the 50th word ("{word_50th}") is: {index_of_50th}')
else:
    print("The lyrics have less than 50 words.")

# Step 4: Replace the first verse with the last verse
verses = lyrics.strip().split("\n")  # Split the lyrics into verses (assuming they're separated by newlines)
if len(verses) >= 2:
    first_verse = verses[0]
    last_verse = verses[-1]
    # Replace the first verse with the last one
    new_lyrics = lyrics.replace(first_verse, last_verse, 1)
    print("\nLyrics after swapping the first and last verse:")
    print(new_lyrics)
else:
    print("The lyrics do not have enough verses to perform a swap.")

# Step 5: Concatenate two verses together to create your own song
if len(verses) >= 3:
    new_song = verses[1] + "\n" + verses[2]  # Concatenating the second and third verses
    print("\nNew song created by concatenating two verses:")
    print(new_song)
else:
    print("The lyrics do not have enough verses to concatenate.")

The title "die with a smile" appears 3 times in the song.
The index of the 50th word ("last") is: 248

Lyrics after swapping the first and last verse:

I'd wanna be next to you
Where you and I had to say goodbye
And I don't know what it all means
But since I survived, I realized

Wherever you go, that's where I'll follow
Nobody's promised tomorrow
So I'ma love you every night like it's the last night
Like it's the last night

If the world was ending
I'd wanna be next to you
If the party was over
And our time on Earth was through
I'd wanna hold you just for a while
And die with a smile
If the world was ending
I'd wanna be next to you

(Ooh, ooh)

Ooh, lost, lost in the words that we scream
I don't even wanna do this anymore
'Cause you already know what you mean to me
And our love's the only war worth fighting for

Wherever you go, that's where I'll follow
Nobody's promised tomorrow
So I'ma love you every night like it's the last night
Like it's the last night

If the world was ending
I'd wanna be next to you
If the party was over
And our time on Earth was through
I'd wanna hold you just for a while
And die with a smile
If the world was ending
I'd wanna be next to you

Right next to you
Next to you
Right next to you
Oh-oh

If the world was ending
I'd wanna be next to you
If the party was over
And our time on Earth was through
I'd wanna hold you just for a while
And die with a smile
If the world was ending
I'd wanna be next to you
If the world was ending
I'd wanna be next to you


New song created by concatenating two verses:
Where you and I had to say goodbye
And I don't know what it all means
%%js
// Popcorn Hack 2: Concat a flower with an animal to make a new creature, use both quotes and backtick/ template literal to display this, set the new creature as a variable, write a short story about your creature using variables, multiple lines, dialog, and apostrophes. You can use either quotes or backtick
// Step 1: Define a flower and an animal
const flower = "Rose";
const animal = "Tiger";

// Step 2: Concatenate to make a new creature using template literals and quotes
let newCreature = flower + " " + animal;
console.log("The new creature is: " + newCreature);

// Using backticks (template literal)
let newCreatureTemplate = `${flower} ${animal}`;
console.log(`The new creature (using backticks) is: ${newCreatureTemplate}`);

// Step 3: Write a short story about the creature using variables and dialog
let name = "Tyler";  // New creature's name
let color = "crimson";
let habitat = "the enchanted jungle";

let story = `${name} was a rare creature with petals as soft as silk, but claws sharp enough to tear through steel. 
One evening, as the sun set in ${habitat}, ${name} roared, its ${color} fur glowing in the twilight.

"Why does the world fear me?" ${name} whispered to the stars, staring at the night sky.

A wise owl swooped down and replied, "They fear what they don't understand, ${name}. But your beauty is undeniable."

With a sigh, ${name} said, 'Maybe one day, they’ll see me for who I am, not for the fearsome stories they’ve heard.'`;

console.log(story);


<IPython.core.display.Javascript object>
#Final Hack: Text Analyzer Python

# Step 1: Accept input from the user
user_input = input("Enter multiple sentences or words: ")

# Step 2: Display original string
print("\nOriginal String:")
print(user_input)

# Step 3: Count total characters (including spaces)
total_characters = len(user_input)
print(f"\nTotal characters (including spaces): {total_characters}")

# Step 4: Find the longest word and its character length
words = user_input.split()  # Split the input into words
longest_word = max(words, key=len)
print(f"\nLongest word: '{longest_word}' with {len(longest_word)} characters")

# Step 5: Display the string reversed
reversed_string = user_input[::-1]
print("\nReversed String:")
print(reversed_string)

# Step 6: Find the middle word/character (excluding spaces and special characters)
import re

# Remove spaces and special characters
cleaned_string = re.sub(r'[^A-Za-z0-9]+', '', user_input)
total_clean_characters = len(cleaned_string)

# Find the middle character
middle_index = total_clean_characters // 2
middle_character = cleaned_string[middle_index]

print(f"\nMiddle character (excluding spaces and special characters): '{middle_character}'")

# Step 7: Find the middle word
if len(words) % 2 == 0:
    middle_word = words[len(words) // 2 - 1]
else:
    middle_word = words[len(words) // 2]

print(f"\nMiddle word: '{middle_word}'")


Original String:


Total characters (including spaces): 0

Reversed String:




---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

Cell In[7], line 30
     28 # Find the middle character
     29 middle_index = total_clean_characters // 2
---> 30 middle_character = cleaned_string[middle_index]
     32 print(f"\nMiddle character (excluding spaces and special characters): '{middle_character}'")
     34 # Step 7: Find the middle word


IndexError: string index out of range