Python Strings

A String means a sequence of characters. Python strings are always surrounded by single quote(') or double qoute (") even triple quote(''') characters. such as 'hello' or "hello" or '''hello'''. By the quotation mark Python knows where the string begins and ends.

Example:

  1. my_string1 = "Hello World!"
  2. my_string2 = 'Hello World!'
  3. my_string3 = '''Hello World!'''
  4. my_string4 = """Hello World!"""
  5. print(my_string1)
  6. print(my_string2)
  7. print(my_string3)
  8. print(my_string4)

Output:

Hello World!
Hello World!
Hello World!
Hello World!

Indexing and Slicing of a String

You can access the character of a string using the bracket. The expression in brackets is called an index. In python the index always starts with 0. If you specify an index you will get the character at that position in the string.

Example:

  1. country = "Bangladesh"
  2. letter = country[1]
  3. print(letter)

Output:

a

The example extracts the character at index position 1 from the country variable and assigns it to the letter variable. As the index starts with 0 the output of the example is a.

You can also specify a range from one index to another.

Example:

  1. country = "Bangladesh"
  2. letter = country[2:5]
  3. print(letter)

Output:

ngl

Negative indexing can also be used. It starts from the last value. To get the characters from 5 to 1 position you should start the count from the end of the string.

Example:

  1. country = "Bangladesh"
  2. letter = country[-5:-2]
  3. print(letter)

Output:

ade

Length of a String

To get length of a string there is a built-in function in Python. len() is the built-in function that returns the number of character in a string.

Example:

  1. country = "Bangladesh"
  2. length = len(country)
  3. print(length)

Output:

10

Useful String Methods

Python has a set of built-in methods. Here some useful methods are described:

upper(): This method returns a new string where all the letter in the original string have been converted to uppercase.

Example:

  1. country = "Bangladesh"
  2. country = country.upper()
  3. print(country)

Output:

BANGLADESH

lower(): This method returns a new string where all the letter in the original string have been converted to lowercase.

Example:

  1. country = "Bangladesh"
  2. country = country.lower()
  3. print(country)

Output:

bangladesh

strip(): This method removes whitespace from the beginning or the end.

Example:

  1. country = " Bangladesh "
  2. country = country.strip()
  3. print(country)

Output:

Bangladesh

replace(): This method removes whitespace from the beginning or the end.

Example:

  1. mystring = "Rahim"
  2. mystring = mystring.replace("R","F")
  3. print(mystring)

Output:

Fahim

split(): This method removes whitespace from the beginning or the end.

Example:

  1. mystring = "I love my country"
  2. mystring = mystring.split(",")
  3. print(mystring)

Output:

['I', ' love', ' my', ' country']

String Methods

There are a set of built-in methods in python which you can use on strings.

Method Description
capitalize() Converts first character to Capital Letter
center() Returns a centered string
casefold() Converts to casefolded strings
count() Returns occurrences of substring in string
endswith() Checks if String Ends with the Specified Suffix
expandtabs() Replaces Tab character With Spaces
encode() Returns encoded string of given string
find() Returns the index of first occurrence of substring
format() formats string into precise output
index() Returns Index of Substring
isalnum() Checks Alphanumeric Character
isalpha() Checks if All Characters are Alphabets
isdecimal() Checks Decimal Characters
isdigit() Checks Digit Characters
isidentifier() Checks for Valid Identifier
islower() Checks if all Alphabets in a String are Lowercase
isnumeric() Checks Numeric Characters
isprintable() Checks Printable Character
isspace() Checks Whitespace Characters
istitle() Checks for Titlecased String
isupper() returns if all characters are uppercase characters
join() Returns a Concatenated String
ljust() returns left-justified string of given width
rjust() returns right-justified string of given width
lower() returns lowercased string
upper() returns uppercased string
swapcase() swap uppercase characters to lowercase; vice versa
lstrip() Removes Leading Characters
rstrip() Removes Trailing Characters
strip() Removes Both Leading and Trailing Characters
replace() Replaces Substring Inside
rfind() Returns the Highest Index of Substring
rindex() Returns Highest Index of Substring
startswith() Returns true if the string starts with the specified value
title() Converts the first character of each word to upper case
zfill() Fills the string with a specified number of 0 values at the beginning
translate() returns mapped charactered string