Home arrow Python arrow Page 2 - Python: More Fun with Strings

Working with String Methods - Python

In our last article we left off discussing ways to manipulate strings in Python, like concatenating, repeating, escape characters, and so forth. In this episode we will pick up where we left off and learn some more ways to work with strings, starting with the comparison method.

TABLE OF CONTENTS:
  1. Python: More Fun with Strings
  2. Working with String Methods
  3. Capital Idea(s)
  4. Putting Our Strings to the Test
  5. Converting Data and Sorting
By: James Payne
Rating: starstarstarstarstar / 5
February 04, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

There are many built-in string methods in Python. We will cover a few of them here, and all of them in a future article.

A method saves the programmer from having to program the same code over and over again. Instead of typing a bunch of code to say, count the number of a's in a string, all the programmer has to do is call the appropriate method. In the following example, we are going to find out how many times each vowel appears in a string. Type the following into your python shell command prompt:


book='It was a dark and stormy night, the wind blowing through the trees, and somewhere, out there in the far off teinkling of city lights, Britney Spears was busy being a bad mother...'

book.count('a')

book.count('e')

book.count('i')

book.count('o')

book.count('u')

book.count('y')

The method will count each of the vowels and return a result, like the one below:


>>> book.count('a')

10

>>> book.count('e')

15

>>> book.count('i')

10

>>> book.count('o')

8

>>> book.count('u')

3

>>> book.count('y')

4

We can also count words within the document, like so:


>>> book.count('the')

5

>>> book.count('and')

2

>>> book.count('was')

2



 
 
>>> More Python Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: