Python String Exercises: Practice for Text Manipulation

thumb_up 1  ·  sell Python string exercises, Text manipulation exercises in Python, String manipulation practice in Python

Example 1

Python program to find number of vowels in a given string.

 
mystr = "All animals are equal. Some are more equal" vowels = "aeiou" count=0 for x in mystr: if x.lower() in vowels: count+=1 print ("Number of Vowels:", count)

It will produce the following output −

Number of Vowels: 18

Example 2

Python program to convert a string with binary digits to integer.

 
mystr = '10101' def strtoint(mystr): for x in mystr: if x not in '01': return "Error. String with non-binary characters" num = int(mystr, 2) return num print ("binary:{} integer: {}".format(mystr,strtoint(mystr)))

It will produce the following output −

binary:10101 integer: 21

Change mystr to '10, 101'

binary:10,101 integer: Error. String with non-binary characters

Example 3

Python program to drop all digits from a string.

 
digits = [str(x) for x in range(10)] mystr = 'He12llo, Py00th55on!' chars = [] for x in mystr: if x not in digits: chars.append(x) newstr = ''.join(chars) print (newstr)

It will produce the following output −

Hello, Python!

Exercise Programs

  • Python program to sort the characters in a string

  • Python program to remove duplicate characters from a string

  • Python program to list unique characters with their count in a string

  • Python program to find number of words in a string

  • Python program to remove all non-alphabetic characters from a string

 

 

The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back