Python String
Python String- Avid Python

Python String

A string in Python or any other programming language is nothing but a collection of characters. We can also say that a String is an array of characters stored in a sequence.

How strings are stored in Python?

Python strings are stored internally as Unicode sequences. Here, Unicode is nothing but a numeric value that we get after encoding a character. So, we can say that python stores strings as array of bytes representing Unicode characters.

How to create a string in Python?

We can create strings by enclosing characters inside single quotes or double quotes. To create a multi-line string, we need to enclose characters inside triple quotes. However, we also use triple quotes to create Docstrings. For example,

#using single quotes
string1 = 'python programming'
print(type(string1))
print(string1)

#using double quotes
string2 = "programming in python"
print(type(string2))
print(string2)

#using triple quotes for multiline strings
string3 = '''Python is very suitable
language for Data Science
and Machine learning'''
print(type(string3))
print(string3)

Output:

<class 'str'>
python programming
<class 'str'>
programming in python
<class 'str'>
Python is very suitable
language for Data Science
and Machine learning

How to access the characters of a python string?

We can access the characters of the string using Indexing and Slicing.

Indexing

As we know, in all programming languages there is a concept of Indexing whenever there is a sequence of objects. Also, indexing of a sequence starts from 0. In python strings also indexing starts from 0. So, we can say that index of first character in string is 0, second character is 1 and so on.

In addition, Python also supports negative indexing. In negative indexing characters are accessed from end of the string. Here, index of last element is -1, second last element is -2 and so on.

Therefore, we can access any character from a string using indexing. For example,

mystr = "python programming"
print(mystr[0]) #first character of string
print(mystr[1])
print(mystr[2])
print(mystr[3])
print(mystr[10])
print('negative indexing:')
print(mystr[-1])
print(mystr[-5])

Output:

p
y
t
h
g
negative indexing:
g
m

Slicing

Slicing is just like taking slice (or part) of a full sequence. Instead of a single character we can access a part of string using slicing. We can use slicing with the help of slicing operator colon(:). For example,

Syntaxstring_name[start : end : step]

mystr = "python for data science"

#string from index 0 to index before 6
print(mystr[:6])

#string from index 8 upto end
print(mystr[8:])

#string from index 3 to index before 15
print(mystr[3:15])

#string from index 2 to 20 with 3 steps
print(mystr[2:20:3])

Output:

python
or data science
hon for data
tnodac

How to find length of a python string?

We can determine length of Python string using len() function. This function returns the length of the string. For example,

mystr = "Hello Python"
print(len(mystr))

Output:

12

Can we modify a python string?

In python strings are Immutable. Once we define a string, we cannot modify the string. That means any character of a string cannot be changed or removed. Also, we cannot extend or shorten a string once defined. Therefore, if we attempt to modify a string it will throw an error. For example,

mystr = "wordpress"
try:
    mystr[3] = "k"
except Exception as e:
    print(e)
print(mystr)

Output:

'str' object does not support item assignment
wordpress

However, we can delete an entire string. To delete an entire string, we simply need to use del function. It will delete the string. For example,

mystr = 'coding is best'
print(mystr)
del(mystr)
try:
    print(mystr)
except Exception as e:
    print(e)

Output:

coding is best
name 'mystr' is not defined

What operations can be performed on a python string?

Concatenation

Concatenation is a simply operation which concatenates or combines two strings. To join two strings, we use concatenation (+) operator. For example,

A = "Artificial "
B = "Intelligence"
mystr = A + B
print(mystr)

Output:

Artificial Intelligence

Repetition

When we need to concatenate multiple copies of same string its called Repetition. To repeat a string, we use repetition (*) operator. For example,

mystr = "Python"
print(mystr*4)

Output:

PythonPythonPythonPython

Membership

Membership operation simply checks the existence of a character or sub string within a string. In other words, membership operation checks whether a sub sequence is member of a sequence or not.

For this operation we use membership operator (in). Whenever it finds the presence of a sub sequence inside the string it returns True and False otherwise. For example,

mystr = "Python for Data Science"
print('Data' in mystr)
print('Py' in mystr)
print('m' in mystr)

Output:

True
True
False

Iteration

Iteration is a simple operation in all the programming languages. It means iterating over all the objects of a sequence one by one. In case of Python strings, we can iterate over all the characters of the string. Iteration can be performed using loops. Most of the times we use for loop for iteration. For example,

mystr = "Anaconda"
for character in mystr:
    print(character)

Output:

A
n
a
c
o
n
d
a

How to format a Python string?

Formatting a string refers to arrange or set a string in the desired manner. In Python, formatting of strings can be done by using Escape Sequences and format() method.

Escape Sequences

Escape sequences are a set of characters that we use in Python strings. We use them to insert some special characters or to ignore some characters. Following are some examples of escape sequences.

Escape sequenceUseExample
\newlineIt ignores the new line in a multiline stringprint("one\
    two\
    three")

Output:
one two three
\\To insert a backlash in stringprint("left\\right")
Output:
left\right
\’To insert a single quote within the stringprint('Bunny\'s Kitchen')
Output:
Bunny's Kitchen
\”To insert a double quote within the stringprint("He said \"Wow\"")
Output:
He said "Wow"
\nTo insert a newlineprint("Above\nBelow")
Output:
Above
Below
\tTo insert a tabprint("Left\tRight")
Output:
Left Right
\bTo insert a backspaceprint("Good and \bBad")
Output:
Good andBad
\oooTo insert a string with octal values
where o is octal number
print("\132\102\147\152\160")
Output:
ZBgjp
\xHHTo insert a string with hex valueprint("\x51\x75\x5a\x6c\x6b")
Output:
QuZlk

format() method

We can format strings using format() method available in Python. format() method is used to place different objects inside a string. A string can be defined with curly braces {} acting as placeholder. Now, using format method we can place objects inside these placeholders according to our requirement.

Here, formatting can be of two types first using Positional arguments and second using Keyword argument. For example,

#using simple curly braces
print("{} scored {}% in Mathematics".format('Aman', 94))

#using positional arguments
print("Kajal and {1} both wants to visit {0}".format('Shimla', 'Pooja'))

#using keyword arguments
print("One: {a}, Two: {b}, Three: {c}".format(a=1, c=3, b=2))

Output:

Aman scored 94% in Mathematics
Kajal and Pooja both wants to visit Shimla
One: 1, Two: 2, Three: 3

What are the different string methods in Python?

There are several methods available in Python that we can use on strings. Following table represents the list of all the methods.

MethodDescription
capitalize()This method capitalize the first character of the string or we can say it converts first character to upper case
casefold()This method coverts the entire string into lower case
center(width, character)This method will center align the string up to given width and fills remaining space with optional character value
count(substring, start, end)This method returns the occurrences of given substring in a string from ‘start’ to ‘end’ index
encode()This methods encodes the given string and returns its encoded version
endswith(suffix, start, end)This method checks whether a string is ending with given suffix from ‘start’ to ‘end’ index
expandtabs(tabsize)This method sets the tab size to specified number of spaces
find(substring, start, end)This method checks the string from ‘start’ to ‘end’ index for the occurrence of given substring. Returns index of element if found or returns -1 when not found
index(substring, start, end)This method checks the string from ‘start’ to ‘end’ index for the occurrence of given substring. Raises exception when element not found
isalnum()This method returns True if all characters of string are alphabets and numbers only, otherwise False
isalpha()This method returns True if all characters of string are alphabets, otherwise False
isdecimal()This method returns True if all characters of string are decimals, otherwise False
isdigit()This method returns True if all characters of string are digits, otherwise False
isidentifier()This method checks if a string is a valid identifier and returns True, otherwise False
islower()This method returns True if all characters of string are in lower case, otherwise False
isnumeric()This method returns True if string contains only numeric values, otherwise False
isprintable()This method checks whether each character of string is printable and returns True, otherwise False
isspace()This method returns True if all characters of string are whitespace, otherwise False
istitle()This method checks if the string is a Title having properties of a title
isupper()This method returns True if all characters of string are in upper case, otherwise False
join(sequence)This method takes all objects of given sequence and joins them into a single string
ljust(width, character)This method will left align the string to the specified width, using a specified character to fill the space
lower()This method converts the entire string into lower case
lstrip()This method removes all leading white spaces of a string
partition(substring)This method searches for the first occurrence of specified substring inside the string and returns the tuple containing part of string before that substring, substring and part of string after that substring
replace(old, new, count)This method replaces ‘old’ phrase with ‘new’ phrase in the string. By default it replaces all occurrence of ‘old’ phrase or if specified ‘count’ number of times
rfind(val, start, end)This method finds the index of given value from right of the string. Start and end are optional to specify index of search. It returns -1 if occurrence not found
rindex(val, start, end)This method finds the index of given value from right of the string. Start and end are optional to specify index of search. It raises exception if occurrence not found
rjust(width, character)This method will right align the string to the specified width, using a specified character to fill the space
rpartition()This method searches for the last occurrence of specified substring inside the string and returns the tuple containing part of string before that substring, substring and part of string after that substring
rstrip()This method removes all trailing white spaces of a string
rsplit(separator, maxsplit)This method splits the string into list from right direction. If specified it will split string into ‘maxsplit’ elements
split()This method splits the string into list from left direction. If specified it will split string into ‘maxsplit’ elements
splitlines()This method splits the string into list and splitting is done from line breaks
startswith(substring, start, end)This method checks whether a string starts with given substring. Start and end are optional to specify index of search.
strip()This method removes all the trailing and leading spaces from a string
swapcase()This method inverts the case of all characters of the string
title()This method converts first letter of every word of string to uppercase
upper()This method converts the entire string into upper case
zfill(length)This method adds zeros (0) at the beginning of the string, until it reaches the specified length.
String Methods- Avid Python

Conclusion

In this article, we have discussed Python strings and their properties. We have also discussed various operations that we can perform on the string and all built-in string methods. To read about another data structure named tuple, visit this article on python tuple. You can also read about python list, the mutable version of tuples.

Piyush Sharma

Third-year computer engineering student inclined towards the latest trends and technology. Always keen to explore and ready to learn new things. To read more articles on Python, stay tuned to avidpython.com.

Leave a Reply