0% found this document useful (0 votes)
42 views3 pages

Ristructer

The document contains 5 Python exercises: 1) Print the index of each character in a string, 2) Count repeated characters in a string and print those that repeat more than once, 3) Display formatted text with a maximum width, 4) Add 'ing' or 'ly' to the end of a string depending on its length and ending, and 5) Remove the characters at odd index positions from a string.

Uploaded by

danial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

Ristructer

The document contains 5 Python exercises: 1) Print the index of each character in a string, 2) Count repeated characters in a string and print those that repeat more than once, 3) Display formatted text with a maximum width, 4) Add 'ing' or 'ly' to the end of a string depending on its length and ending, and 5) Remove the characters at odd index positions from a string.

Uploaded by

danial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Strings Exercises

1. Write a Python program to print the index of the character in a string.

str1 = "Saya belajar di UPSI"

for index, char in enumerate(str1):

print("Current character", char, "position at", index )

2. Write a python program to count repeated characters in a string.

import collections

str1 = 'sayaadalahpelajarupsi'

d = collections.defaultdict(int)

for c in str1:

d[c] += 1

for c in sorted(d, key=d.get, reverse=True):

if d[c] > 1:

print('%s %d' % (c, d[c]))


3. Write a Python program to display formatted text (width=50) as output.

import textwrap

sample_text = '''

Python is a widely used high-level, general-purpose, interpreted,

dynamic programming language. Its design philosophy emphasizes

code readability, and its syntax allows programmers to express

concepts in fewer lines of code than possible in languages such

as C++ or Java.

'''

print()

print(textwrap.fill(sample_text, width=70))

print()
4. Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the
given string already ends with 'ing' then add 'ly' instead. If the string length of the given string is less
than 3, leave it unchanged.

def add_string(str1):

length = len(str1)

if length > 2:

if str1[-3:] == 'ing':

str1 += 'ly'

else:

str1 += 'ing'

return str1

print(add_string('ab'))

print(add_string('abc'))

print(add_string('string'))

5. Write a Python program to remove the characters which have odd index values of a given string.

def odd_values_string(str):

result = ""

for i in range(len(str)):

if i % 2 == 0:

result = result + str[i]

return result

print(odd_values_string('abcdef'))

print(odd_values_string('python'))

You might also like