Python Strings

A string is a character of sequence in any programming language. Python provides a powerful set of operations and functions over the string that makes python fun to program on strings.

How to create a string in Python?

Python strings are declared using a single quote or double quote or triple quote.

# Single Line Strings
name = "John Doe" # Double Quote
friend = 'Ricky Ponting' # Single Quote

# Multiline String separated by a black slash at the end of each line
hello = "Hello\
world"

# Using triple quotation marks
welcome = """welcome 
to Programming 
World of Python"""

How to access values of a string in Python?

We can use a slice operator [] and [:] to get a substring from a string in Python. The index of string starts from 0 and -1 represents the end of the string. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator.

Let us explore some examples:-

str = 'Hello World!'
print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:]      # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str + "TEST" # Prints concatenated string

How to change or delete a string?

Python strings are immutable, meaning you cannot change once it has been created. One thing you can do is update the variable with another string. For example, the following operation is not permitted.

name = 'John'
name[0] = 'd'
print(name)

It results in an error as :-

Traceback (most recent call last):
File “testing.py”, line 2, in <module>
name[0] = ‘d’
TypeError: ‘str’ object does not support item assignment
We can, however, assign a new value to string:-
name = 'John'
name = 'Alex'
print(name)

We cannot delete or remove characters from a string. But we can delete whole string using a del function.

What are the Python String Operations?

Python provides the various operator to work on Python string data type. Let’s have a look at the information provided in the table as below:-

OperatorDescription
+Concatenation Operator – Joins two strings
*Repetition Operator – Repeats multiples copies of the same string
[]Slice Operator – used to access substring in a string
[:]Range Slice Operator – get characters from a specified range
inMembership Operator – used to check if a particular sub-string is present in the specified string.
not inMembership Operator – used to check if a particular sub-string is not present in the specified string.
r/RRaw string  – used in the cases where we need to print the actual meaning of escape characters
%String formatting operator

What are escape characters in Python?

An escape character gets interpreted; in a single quoted as well as double-quoted strings. Here is the list of escape sequences:-

Escape SequenceDescription
\nNewline
\\Backslash
\’Single Quote
\”Double Quote
\aAlert or Bell sign
\bBackspace
\fFormfeed
\rCarriage Return
\tHorizontal Tab
\vVertical Tab
\oooA character with octal value
\xHHA character with hexadecimal value HH

Examples for Escape characters in Python

print("This is \"double quote\" print")
print("C:\\Python32\\Lib")
print("This is printed\nin two lines")
print("This is \x48\x45\x58 representation")

Output:-

This is “double quote” print
C:\Python32\Lib
This is printed
in two lines
This is HEX representation

How to format strings in Python?

We can use % operator to format string. It gives the C like functionality in Python. Let’s jump right to an example:-

print("Hello, My name is %s and I live at %s" % ('John Doe', 'New York'))
# Output :- 
# Hello, My name is John Doe and I live at New York

Format symbols and their use is tabulated below:-

Format SpecifierConversion
%cCharacter
%sString conversion
%isigned decimal integer
%dsigned decimal integer
%uunsigned decimal integer
%ooctal integer
%xhexadecimal integer (lowercase letters)
%Xhexadecimal integer (uppercase letters)
%eexponential notation (with lowercase ‘e’)
%Eexponential notation (with uppercase ‘E’)
%ffloating point real number
%gthe shorter of %f and %e
%Gthe shorter of %f and %E

The format() Method for Formatting Strings

We can use a better version of the format method for formatting string.

Let us explore with some examples:-

# default arguments
print("Hello {}, your marks roll number is {}.".format("John", 79))

# positional arguments
print("Hello {0}, your roll number is {1}.".format("John", 79))

# keyword arguments
print("Hello {name}, your roll number is {roll}.".format(name="John", roll=79))

# mixed arguments
print("Hello {0}, your roll number is {roll}.".format("John", roll=79))

The output of the above program is:-

Hello John, your marks roll number is 79.
Hello John, your roll number is 79.
Hello John, your roll number is 79.
Hello John, your roll number is 79.

What are the built-in string methods to work with string in Python?

Python includes a ton of built-in methods to work with strings. We can call the method using string.method() and get the desired output.

MethodDescription
capitalize()Capitalizes the first letter of a string’
casefold()Returns a version of string suitable for case-less comparisons.
center(width, fillchar)Returns a space-padded string with the original string centered to a total of width columns.
count(str, beg= 0,end=len(string))Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given.
decode(encoding=’UTF-8′,errors=’strict’)Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding.
encode(encoding=’UTF-8′,errors=’strict’)Returns encoded string version of string; on an error, the default is to raise a ValueError unless errors is given with ‘ignore’ or ‘replace’.
endswith(suffix, beg=0, end=len(string))Determines if the string or a substring of string (if starting index beg and ending index end are given) ends with suffix; returns true if so and false otherwise.
expandtabs(tabsize=8)Expands tabs in the string to multiple spaces; defaults to 8 spaces per tab if tab size not provided.
find(str, beg=0 end=len(string))Determine if str occurs in a string or in a substring of the string if starting index beg and ending index end are given returns index if found and -1 otherwise.
index(str, beg=0, end=len(string))Same as find(), but raises an exception if str not found.
isalnum()Returns true if a string has at least 1 character and all characters are alphanumeric and false otherwise.
isalpha()Returns true if a string has at least 1 character and all characters are alphabetic and false otherwise.
isdigit()Returns true if a string contains only digits and false otherwise.
islower()Returns true if a string has at least 1 cased character and all cased characters are in lowercase and false otherwise.
isnumeric()Returns true if a Unicode string contains only numeric characters and false otherwise.
isspace()Returns true if a string contains only whitespace characters and false otherwise.
istitle()Returns true if a string is properly “titlecased” and false otherwise.
isupper()Returns true if the string has at least one cased character and all cased characters are in uppercase and false otherwise.
join(seq)Merges (concatenates) the string representations of elements in sequence seq into a string, with separator string.
len(string)Returns the length of the string
ljust(width[, fillchar])Returns a space-padded string with the original string left-justified to a total of width columns.
lower()Converts all uppercase letters in a string to lowercase.
lstrip()Removes all leading whitespace in a string.
maketrans()Returns a translation table to be used in translate function.
max(str)Returns the max alphabetical character from the string str.
min(str)Returns the min alphabetical character from the string str.
replace(old, new [, max])Replaces all occurrences of old in string with new or at most max occurrences if max given.
rfind(str, beg=0,end=len(string))Same as find(), but search backward in a string.
rindex( str, beg=0, end=len(string))Same as index(), but search backward in a string.
rjust(width,[, fillchar])Returns a space-padded string with the original string right-justified to a total of width columns.
rstrip()Removes all trailing whitespace of string.
split(str=””, num=string.count(str))Splits string according to delimiter str (space if not provided) and returns the list of substrings; split into at most num substrings if given.
splitlines( num=string.count(‘\n’))Splits a string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.
startswith(str, beg=0,end=len(string))Determines if a string or a substring of string (if starting index beg and ending index end are given) starts with substring str; returns true if so and false otherwise.
strip([chars])Performs both lstrip() and rstrip() on string.
swapcase()Inverts case for all letters in string.
title()Returns “titlecased” version of string, that is, all words begin with uppercase and the rest are lowercase.
translate(table, deletechars=””)Translates string according to translation table str(256 chars), removing those in the del string.
upper()Converts lowercase letters in a string to uppercase.
zfill(width)Returns original string left-padded with zeros to a total of width characters; intended for numbers, zfill() retains any sign given (less one zero).
isdecimal()Returns true if a Unicode string contains only decimal characters and false otherwise.

Let us checkout some of these methods in action:-

test = "Python is fun to program"
print("john Doe".capitalize())
print("John Doe".upper())
print(test.split())
print("Length of test string is:", len(test))
print(test.replace('fun', 'awesome'))
John Doe
JOHN DOE
[‘Python’, ‘is’, ‘fun’, ‘to’, ‘program’]
Length of test string is: 24
Python is awesome to program
SHARE Python Strings

You may also like...

Leave a Reply

Your email address will not be published.

Share