Home arrow Python arrow Page 3 - String Manipulation

Numbers and spaces - Python

Programmers need to know how to manipulate strings for a variety of purposes, regardless of the programming language they are working in. This article will explain the various methods used to manipulate strings in Python.

TABLE OF CONTENTS:
  1. String Manipulation
  2. Splitting strings, making cases
  3. Numbers and spaces
  4. Regular Expressions
By: Peyton McCullough
Rating: starstarstarstarstar / 96
May 02, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

We can check whether a string is alphanumeric:

>>> 'aa44'.isalnum()
True
>>> 'a$44'.isalnum()
False

It is also possible to check whether a string contains only letters:

>>> 'letters'.isalpha()
True
>>> 'letters4'.isalpha()
False

Here's how you check whether a string contains only numbers:

>>> '306090'.isdigit()
True
>>> '30-60-90 Triangle'.isdigit()
False

We can also check whether a string only contains spaces:

>>> '   '.isspace()
True
>>> ''.isspace()
False

Speaking of spaces, we can add spaces on either side of a string. Let's add spaces to the right of a string:

>>> 'A string.'.ljust ( 15 )
'A string.      '

To add spaces to the left of a string, the rjust method is used:

>>> 'A string.'.rjust ( 15 )
'      A string.'

The center method is used to center a string in spaces:

>>> 'A string.'.center ( 15 )
'   A string.   '

We can strip spaces on either side of a string:

>>> 'String.'.rjust ( 15 ).strip()
'String.'
>>> 'String.'.ljust ( 15 ).rstrip()
'String.'



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

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: