Open In Colab

Python Strings from Scratch !!

Let us understand the fundamentals of strings in python.

alt text

Strings are a sequence of characters which can be stored either as a constant or a different variable. Strings are considered as a datatype. Typically, programmers must enclose strings in quotation marks for the data to recognized as a string and not a number or variable name. Shown below are some of the most used string methods on a daily basis and are one of the most commonly asked interview questions.

For example, declaring a string in python:

In [0]:
# Declaring a string variable
string = "This is a python tutorial"
print(string)
This is a python tutorial
In [0]:
print(type(string))
<class 'str'>

1. Escape sequence in python using strings

In Python strings, the backslash " " is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a new line, and "\r" is a carriage return. Finally, " " can be used to escape itself: "\" is the literal backslash character.

In [0]:
# THis is an escape sequence.
string = "This is a \"Google Colab\" python notebook"
print(string)
This is a "Google Colab" python notebook

2. Accessing the String by an Index

Strings can be accessed by their index in order to get the value. To do this all you have to do is just place the number (index value) inside the pair of square brackets along with the name of the string.

In [0]:
string = "Python"
print(string[2])
t
In [0]:
print(string[5])
n

3. Slicing a string

Slicing a string helps to get a set of characters from a string. This is really helpful when we want to access a particular set of characters in a string. Below are some slicing variants that are useful.

In [0]:
string = "programming"
string
Out[0]:
'programming'

Getting one character of the string

In [0]:
print(string[0:1])
p

Getting the first three characters from the string

In [0]:
print(string[0:3])
pro

Getting the first three characters from the string (Alternate)

In [0]:
print(string[:3])
pro

Getting the last three characters from the string

In [0]:
print(string[-3:])
ing

Getting all characters but excluding three first characters from the string

In [0]:
print(string[3:])
gramming

Getting all characters but excluding the last three characters from the string

In [0]:
print(string[:-3])
programm

Reversing all the characters in a given string

In [0]:
print(string[::-1])
gnimmargorp

Alternative to printing all characters in a string

In [0]:
print(string[::])
programming

4. Splitting a string

Sometimes splitting a string is a handy option because it is one of the easiest ways to convert a string into a list. I know I have not spoken about the list but keep in mind that split converts a string into a list. You can find the material on Python Lists that is written by me which can provide enough idea to master python lists Python Lists

In [0]:
String = "Computer Programming"
String
Out[0]:
'Computer Programming'
In [0]:
type(String)
Out[0]:
str
In [0]:
list = String.split()
list
Out[0]:
['Computer', 'Programming']
In [0]:
type(list)
Out[0]:
list

5. Replacing a string

The Replace function in python is one of the best function that can be applied to strings. For example, shown below is a string "Money" we need to replace the dollar and the comma sign and this can be done as shown below.

In [0]:
Money = '$113,678'
print(Money)
print("===========================")
print(type(Money))
$113,678
===========================
<class 'str'>
In [0]:
Money = Money.replace('$', '')
Money
Out[0]:
'113,678'
In [0]:
Money = Money.replace(',', '')
Money
Out[0]:
'113678'
In [0]:
Money = int(Money)
print(Money)
print("===========================")
print(type(Money))
113678
===========================
<class 'int'>

6. Join

The join function is python is used to join the string according to the specified pattern.

In [0]:
String = "Python Programming is fun"
String
Out[0]:
'Python Programming is fun'
In [0]:
String = " ".join(String)
String
Out[0]:
'P y t h o n   P r o g r a m m i n g   i s   f u n'

7. Capitalize

The capitalize function capitalizes the first character in the word or a string.

In [0]:
string = "programming"
string
Out[0]:
'programming'
In [0]:
string = string.capitalize()
string
Out[0]:
'Programming'

8. Center

The center method returns a string which is padded with the specified character.

In [0]:
string = "python"
string
Out[0]:
'python'
In [0]:
print(string.center(15, '*'))
*****python****

9. Find

The find method returns the index of the given substring position. If the value is not found it returns, -1.

In [0]:
string = "programming"
string
Out[0]:
'programming'
In [0]:
print(string.find('p'))
0
In [0]:
print(string.find('t'))
-1

10. Strip

The strip function strips or removes the white spaces both from the starting and the ending of a string.

In [0]:
string = "     programming is easy    "
string
Out[0]:
'     programming is easy    '
In [0]:
print(string.strip())
programming is easy

Hence above are the very important techniques or functions of Strings in Python. Some of the examples were referred from Python Strings. I have written this tutorial in a simple way such that everybody can understand and master the concepts of Strings in Python without prior programming knowledge or experience.

Thank you