Home arrow Python arrow Page 4 - Python Strings: Spinning Yarns

Dealing with Multiple Words - Python

As the students used to say to Mr. Kotter: "Welcome back, welcome back, welcome back." In our previous article we talked some more about how to manipulate strings in Python, leaving off with indexing and slicing. Here, we will pick up again with slicing, using it to “change” the contents of a string.

TABLE OF CONTENTS:
  1. Python Strings: Spinning Yarns
  2. The Interpolation Operator
  3. Changing Strings with Methods
  4. Dealing with Multiple Words
By: James Payne
Rating: starstarstarstarstar / 3
February 11, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Here is an example where a word appears a number of times:


story='It was a dark and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.'

print story

story=story.replace('dark', 'light')

print story

When we run the code, this is what we get:

  It was a dark and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.

  It was a light and stormy night. There was no light, so it was really light. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother light. Dark.

As you can see, it replaced all instances of the word dark, except the one with the capital D, with light.

This could of course be a problem if we only wanted to replace one specific dark. What if we wanted to change the first word 'dark' to black only? Here is how we would do so:


story='It was a dark and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.'

print story

where=story.find('dark')

print where

story=story[:where] + 'black and ' + story[(where+9):]

print story

Resulting in:

  It was a dark and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.

  9

  It was a black and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.

If we only wanted to replace one of the 'dark' words, we could do it this way as well: (type this into your command prompt)


>>> story.replace('dark','black',1)

'It was a black and stormy night. There was no light, so it was really dark. I mean you could not see anything. We are talking baby chimpanzee trying to wake up its dead mother dark. Dark.'

Well that's all the time we have for now. In our upcoming tutorials we will discuss the various string methods in depth and take a look at math and numbers in Python.

Till then...



 
 
>>> 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: