Soal dan Jawaban Python Exercises 6 1-5
A Practical Introduction to Python Programming
6.11 Exercises
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!!!
SCRIPT Python 1-5.
1.
s = input('Masukkan Isi Karakter: ')
print('(a) Jumlah Total Pada Karakter Didalam String: ',len(s))
print('(b) String Yang Diulang 10 Kali: ',(s*10))
print('(c) Karakter Pertama Pada String: ',s[0])
print('(d) Tiga Karakter Pertama Dari String: ',s[:3])
print('(e) Tiga Karakter Terakhir Dari String: ',s[-3:])
print('(f) String Mundur: ',s[::-1])
if len(s) >= 7:
print('(g) Karakter Ketujuh Dari String: ', s[6])
else:
print(" (g) String nya Tidak Cukup Panjang")
print('(h) String Dengan Karakter Pertama Dan Terakhirnya Akan Dihapus: ',(s[1:len(s)-1]))
print('(i) Semua Huruf Menjadi Besar: ',s.upper())
print('(j) String Dengan Setiap a Diganti dengan e: ',s.replace("a", "e"))
print('(k) Setiap Huruf Akan Diganti Dengan Spasi:',s.replace(s," "))
2.
kata = input("Masukkan string: ")
jumlah_karakter = len(kata)
print(jumlah_karakter)
3.
def check(my_string):
my_string = input('Masukkan string: ')
brackets = ['()', '{}', '[]']
while any(x in my_string for x in brackets):
for br in brackets:
my_string = my_string.replace(br, '')
return not my_string
string = "{[]{()}}"
print(string, "-", "Balanced"
if check(string) else "Unbalanced")
4.
ch = input ( "Masukkan karakter: " )
if ( ch == 'A' or ch == 'a' or ch == 'E' or ch == 'e' or ch == 'I' or ch == 'i' or ch == 'O'
or ch == 'o' or ch == 'U' or ch == 'u' ):
print ( ch , "adalah Vokal" )
else :
print ( ch ,"adalah Konsonan" )
5.
s = input("Masukkan String: ")
position = 1
position2 = len(s)
new_character = '*'
new_character2 ="!!!"
s = s[:position] + new_character + s[position+1:]
s = s[:position2] + new_character2
print(s)