Use Python language to : 1.Assign to a variable in your program a triple-quoted
ID: 3675248 • Letter: U
Question
Use Python language to :
1.Assign to a variable in your program a triple-quoted string that contains a paragraph of text — a poem, a speech, a nursery rhyme, etc. Write a function which removes all punctuation from the string and counts the number of words in your text. Print the original text without punctuation and a count of the number of words. Include as punctuation at least commas, periods, semicolons, and exclamation marks. Do not use any built-in functions on this
2. Write a function that counts the number of times that a substring occurs in a given string. Let the user run this as long as they would like. Do not use any built-in functions on this
Explanation / Answer
input_string = ''
def count_remove_punchuation():
count = 0
punchuation = [',',';',':','!','.']
final_string = ''
for i in input_string:
if i not in punchuation:
final_string+=i
if i == " ":
count+=1
print "string without punchutaiton:", final_string
print "count of words in final string:",count+1
def count_substring():
while (1):
print "enter the substring"
input_substring = raw_input()
len_substring = len(input_substring)
count = 0
for i in range(0, len(input_string)):
if input_string[i:i+len_substring] == input_substring:
count+=1
print "occurence count", count
print "type e to exit/enter to continue"
char = raw_input()
if char == 'e':
break
else:
continue
print "enter the string"
input_string = raw_input()
count_remove_punchuation()
count_substring()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.