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

Capital Idea(s) - 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

We can also use methods to control the appearance of strings, as in the following examples. In our first code, we will make the first letter of our string uppercase, using the capitalize method():


>>> book.capitalize()

'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...'

You will note that nothing really changed since our text was already capitalized to begin with.

In this next example we will use the title method() to capitalize the first letter of every word:


>>> book.title()

'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...'

And to make every letter uppercase, we use the upper method():


>>> book.upper()

'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...'

I know, I know...hold back all your Britney is a Good Person YouTube videos. We're almost done.

And of course we can also change everything to lowercase, using the lower() method:


>>> book.lower()

'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...'

Also, you can switch between upper and lower using the swapcase() method. In or previous example, everything was changed to lower case. We can now use swapcase to switch back to uppercase. If we wanted to switch back to lowercase after that, we could again use switchase:


>>> book.swapcase()

'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...'

As I said before, these are only a few of the string methods available; we will discuss the rest at a later date.



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