• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / String Manipulation in Python

String Manipulation in Python

Author: PFB Staff Writer
Last Updated: August 28, 2020

Overview

A string is a list of characters in order.

A character is anything you can type on the keyboard in one keystroke,
like a letter, a number, or a backslash.

Strings can have spaces:

"hello world".

An empty string is a string that has 0 characters.

Python strings are immutable

Python recognize as strings everything that is delimited by quotation marks
(” ” or ‘ ‘).

String Manipulation

To manipulate strings, we can use some of Pythons built-in methods.

Creation

word = "Hello World"

>>> print word
Hello World

Accessing

Use [ ] to access characters in a string

word = "Hello World"
letter=word[0]

>>> print letter
H

Length

word = "Hello World"

>>> len(word)
11

Finding

word = "Hello World">>> print word.count('l') # count how many times l is in the string
3

>>> print word.find("H") # find the word H in the string
0

>>> print word.index("World") # find the letters World in the string
6

Count

s = "Count, the number of spaces"

>>> print s.count(' ')
8

Slicing

Use [ # : # ] to get set of letter

Keep in mind that python, as many other languages, starts to count from 0!!

word = "Hello World"

print word[0] #get one char of the word
print word[0:1] #get one char of the word (same as above)
print word[0:3] #get the first three char
print word[:3] #get the first three char
print word[-3:] #get the last three char
print word[3:] #get all but the three first char
print word[:-3] #get all but the three last character

word = "Hello World"

word[start:end] # items start through end-1
word[start:] # items start through the rest of the list
word[:end] # items from the beginning through end-1
word[:] # a copy of the whole list

Split Strings

word = "Hello World"

>>> word.split(' ') # Split on whitespace
['Hello', 'World']

Startswith / Endswith

word = "hello world"

>>> word.startswith("H")
True

>>> word.endswith("d")
True

>>> word.endswith("w")
False

Repeat Strings

print "."* 10 # prints ten dots

>>> print "." * 10
..........

Replacing

word = "Hello World"

>>> word.replace("Hello", "Goodbye")
'Goodbye World'

Changing Upper and Lower Case Strings

string = "Hello World"

>>> print string.upper()
HELLO WORLD

>>> print string.lower()
hello world

>>> print string.title()
Hello World

>>> print string.capitalize()
Hello world

>>> print string.swapcase()
hELLO wORLD

Reversing

string = "Hello World"

>>> print ' '.join(reversed(string))
d l r o W o l l e H

Strip

Python strings have the strip(), lstrip(), rstrip() methods for removing
any character from both ends of a string.

If the characters to be removed are not specified then white-space will be removed

word = "Hello World"

Strip off newline characters from end of the string

>>> print word.strip('
')
Hello World

strip() #removes from both ends
lstrip() #removes leading characters (Left-strip)
rstrip() #removes trailing characters (Right-strip)

>>> word = " xyz "

>>> print word
xyz

>>> print word.strip()
xyz

>>> print word.lstrip()
xyz

>>> print word.rstrip()
xyz

Concatenation

To concatenate strings in Python use the “+” operator.

"Hello " + "World" # = "Hello World"
"Hello " + "World" + "!"# = "Hello World!"

Join

>>> print ":".join(word) # #add a : between every char
H:e:l:l:o: :W:o:r:l:d

>>> print " ".join(word) # add a whitespace between every char
H e l l o W o r l d

Testing

A string in Python can be tested for truth value.

The return type will be in Boolean value (True or False)

word = "Hello World"

word.isalnum() #check if all char are alphanumeric 
word.isalpha() #check if all char in the string are alphabetic
word.isdigit() #test if string contains digits
word.istitle() #test if string contains title words
word.isupper() #test if string contains upper case
word.islower() #test if string contains lower case
word.isspace() #test if string contains spaces
word.endswith('d') #test if string endswith a d
word.startswith('H') #test if string startswith H

If you liked this article, please share it with your friends.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics, Python Strings Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Errorhandling Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Set Difference in Python
  • Convert List of Lists to CSV File in Python
  • Remove Elements From a Set in Python
  • Check if a Python String Contains a Number
  • How to Compare Two Files in Python Line by Line

Copyright © 2012–2022 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us