Question

python Strings Question lyrics = """ You think you've got it, oh, you think you've got...

python Strings Question

lyrics = """
You think you've got it, oh, you think you've got it
But “got it” just don't get it 'til there's nothing at all
We get together, oh, we get together
But separate's always better when there's feelings involved
If what they say is “Nothing is forever”
Then what makes, then what makes, then what makes
Then what makes, what makes, what makes love the exception?
So why oh, why oh, why oh, why oh, why oh
Are we so in denial when we know we're not happy here?
Y'all don't want to hear me, you just want to dance

"""

Use lower() to convert your song to lowercase.

Part 2: Replacing characters that aren't part of words

Use replace to replace ! ( ) , . and other punctuation characters in your song with spaces.

Tip: you can replace each character one by one, or you can make a list of non-letter characters and use a loop to replace each character in turn

Part 3: How many lines, how many words?

Use split() to break your (lowercase, punctuation-free) song into words. How many words does your song have?

Use split() to break your song into lines. What escape character do you need to use? How many lines does your song have?

Part 4: Word Frequency

How many times does each word appear in your song?

Start with your list of words in your song.

Create a dictionary of words, and how many times they appear in the song.

How many unique words are in your song? Print the number of unique words.

Part 5: Table, alignment

Display your word frequency counts in a table. Use string formatting and alignment to space your data into a table with neatly aligned columns.

If you have words with upper and lowercase forms, or words with punctuation in, revisit part 1 and 3 and make sure you lowercase your lyrics and remove all punctuation

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

lyrics = """
You think you've got it, oh, you think you've got it
But “got it” just don't get it 'til there's nothing at all
We get together, oh, we get together
But separate's always better when there's feelings involved
If what they say is “Nothing is forever”
Then what makes, then what makes, then what makes
Then what makes, what makes, what makes love the exception?
So why oh, why oh, why oh, why oh, why oh
Are we so in denial when we know we're not happy here?
Y'all don't want to hear me, you just want to dance

"""

#converting to lower case
lyrics=lyrics.lower()
#creating a list of non alphabets in the text
non_alpha=[c for c in lyrics if not c.isalpha()]
#replacing all occurrances of non alphabets with space
for i in non_alpha:
    lyrics=lyrics.replace(i,' ')

#splitting into words
words=lyrics.split()
#displaying number of words
print('Number of words:',len(words))
#splitting into lines and displaying number of lines
lines=lyrics.split('\n')
print('Number of lines:',len(lines))

#creating an empty dictionary
words_count={}
#looping through words
for i in words:
    #checking if current word is already added to dict
   
if i in words_count:
        #incrementing count
       
words_count[i]+=1
    else:
        #adding as new entry with count 1
       
words_count[i]=1

#displaying number of unique words which will be the length of dict
print('Number of unique words:',len(words_count))

#displaying - character 31 times
print('-'*31)
#printing header, aligning one column to left, other to right
print('{:<15s} {:>15s}'.format('WORD','FREQUENCY'))
#displaying - character 31 times
print('-'*31)
#printing each word and count, sorted by word alphabetically
for i in sorted(words_count):
    print('{:<15s} {:>15d}'.format(i,words_count[i]))

#output

Number of words: 108

Number of lines: 1

Number of unique words: 52

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

WORD                  FREQUENCY

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

all                           2

always                        1

are                          1

at                            1

better                        1

but                           2

dance                         1

denial                        1

don                           2

exception                     1

feelings                      1

forever                       1

get                           3

got                           3

happy                         1

hear                          1

here                          1

if                            1

in                            1

involved                      1

is                            2

it                            4

just                          2

know                          1

love                          1

makes                         6

me                            1

not                           1

nothing                       2

oh                            7

re                            1

s                             3

say                           1

separate                      1

so                            2

t                             2

the                           1

then                          4

there                         2

they                          1

think                         2

til                           1

to                            2

together                      2

ve                            2

want                          2

we                            5

what                          7

when                          2

why                           5

y                             1

you                           5

Add a comment
Know the answer?
Add Answer to:
python Strings Question lyrics = """ You think you've got it, oh, you think you've got...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part 1. Write a program that takes a string as input, strips whitespace and punctuation from...

    Part 1. Write a program that takes a string as input, strips whitespace and punctuation from the words, and converts them to lowercase. Hint: The string module provides strings named whitespace, which contains space, tab, newline, etc., and punctuation which contains the punctuation characters. Let’s see if we can make Python swear: >>> import string >>> print string.punctuation !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ Also, you might consider using the string methods strip, replace and translate (Chapter 2 of "Introducing Python" is a great reference...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • You're to write a C++ program that analyzes the contents of an external file called data.txt....

    You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...

  • PA #1: Word Counter Tabulating basic document statistics is an interesting exercise that leverages your knowledge...

    PA #1: Word Counter Tabulating basic document statistics is an interesting exercise that leverages your knowledge of strings, files, loops, and arrays. In this homework, you must write a C++ program that asks the user for an input and output file. For each line in the input file, write a modified line containing a line number to the output file. Additionally, calculate the number of paragraphs, lines, words, and characters. Write the summary information to the bottom of the output...

  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

  • Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...

    Instructions: Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13...

  • Please Use Python/IDLE language! Also, pleaseee include step by step algorithm. Python 3.4 Hawaiin Words program...

    Please Use Python/IDLE language! Also, pleaseee include step by step algorithm. Python 3.4 Hawaiin Words program help please. Hawaiian words can be intimidating to attempt to pronounce. Words like humuhumunukunukuapua'a look like someone may have fallen asleep on the keyboard. The language is actually very simple and only contains 12 characters; 5 vowels and 7 consonants. We are going to write a program that allows the user to enter a hawaiian word and give the pronunciation of the word or...

  • Question 5: (Annuity Payment) You've got a $25,000 in student loan. if you pay it back...

    Question 5: (Annuity Payment) You've got a $25,000 in student loan. if you pay it back over 15 years at 7% compounded monthly, how much is your monthly loan payments? Question 6: (Perpetuity) What is the present value of a perpetuity: $300 perpetuity discounted back to the present 8%? Showing clearly which EQUATIONS from the textbook could be used to solve the problem mathematically Indicating the detailed steps on how to use FINANCIAL CALCULATOR to solve the problems. You also...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT