In Python 1) Write a program that asks the user to enter a string. The program s
ID: 3715190 • Letter: I
Question
In Python
1) Write a program that asks the user to enter a string. The program should then print the following:
(a) The total number of characters in the string
(b) The string repeated 10 times
(c) The first character of the string (remember that string indices start at 0)
(d) The first three characters of the string
(e) The last three characters of the string
(f) The string backwards
(g) The seventh character of the string if the string is long enough and a message otherwise
(h) The string with its first and last characters removed
(i) The string in all caps
(j) The string with every a replaced with an e
(k) The string with every letter replaced by a space
2. A simple way to estimate the number of words in a string is to count the number of spaces in the string. Write a program that asks the user for a string and returns an estimate of how many words are in the string.
3. People often forget closing parentheses when entering formulas. Write a program that asks the user to enter a formula and prints out whether the formula has the same number of opening and closing parentheses.
4. Write a program that asks the user to enter a word and prints out whether that word contains any vowels.
5. Write a program that asks the user to enter a string. The program should create a new string called new_string from the user’s string such that the second character is changed to an asterisk and three exclamation points are attached to the end of the string. Finally, print new_string. Typical output is shown below:
Enter your string: Qbert
Q*ert!!!
Explanation / Answer
#1
string = input("Please enter the string ")
print "The string you entered is : " + string
#(a) Total numbers of characters in the string
length = len(string)
print "Total numbers of characters in the string", length
#(b) The string repeated 10 times
print "The string repeated 10 times:"
n = 0
while(n < 10):
print string
n+=1
#(c) The first character of the string
print "The first character of the string is " + string[0]
#(d) The first three characters of the string
n = 0
str = ''
while(n < 3):
str+=string[n] + ","
n+=1
#below we have used slice function of the string to remove the last ","
print "The first three character of the string are " + str[0:len(str)-1]
#(e) The last three characters of the string
n = 0
str = ''
while(n < 3):
str+=string[(length-1) - n] + ","
n+=1
#below we have used slice function of the string to remove the last ","
print "The first three character of the string are " + str[0:len(str)-1]
#(f) The string backwards
def reverse(s):
str = ""
for i in s:
str = i + str
return str
print "The string backwards " + reverse(string)
#(g) The seventh character of the string if the string is long enough
if length >=7:
print "Then seventh character of the string is " + string[7]
else:
print "The string is not long enough"
#(h) The string with its first and last characters removed
print "The string with its first and last characters removed " + string[1:length-1]
#(i) The string in all caps
print "The string in all caps is " + string.upper()
#(j) The string with every a replaced with an e
print "The string with every a replaced with an e is " + string.replace("a", "e")
#(k) The string with every letter replaced by a space
for ch in string:
if ch in string:
string = string.replace(ch," ")
print "The string with every letter replaced by a space is " + string
#2 Number of words in a string
string = input("Please enter a string ")
words = string.split(" ")
print len(words)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.