Python String Data Types: An Introduction

Exploring the basics of Python string data type along with code examples.

July 11th, 2022

Image of someone holding a post-it note with the word Python

In Python, the string data type is treated as an object. The string is one of the most important data types in Python; it's a sequence of characters usually enclosed within double quotation marks (" ").

In this article, we will discuss the string data type in Python. Python string is an object data type. There are a lot of built-in functions in Python which are related to the string data type.

What is a String?

A string is a data type in Python that is enclosed within quotation marks (" ") and can perform operations like concatenation, etc.

Example Code:

name="john”print(name)print(type(name))

Output:

john<class ‘str’>

Here we have created a variable named name and stored the value john within double quotation marks (" ") and hence the data type of the variable named name is a string.

There are a lot of built-in functions of string objects in Python and they are discussed in the following lines.

capitalize():

This function will capitalize the first character of the sting. In other words, this will convert the lowercase letters into uppercase (capital) letters, letting the uppercase letters remain unchanged.

Example Code:

name="john”print(name.capitalize())

Output:

John

upper():

This function will capitalize all the characters of the string. In other words, this will convert the lowercase letters into uppercase (capital) letters, letting the uppercase letters remain unchanged.

Example Code:

name="john”print(name.upper())

Output:

JOHN

lower():

This function will convert all the characters of the string to lowercase. If a character is uppercase it will be converted into lowercase and if it is in lowercase then it will remain unchanged.

Example Code:

name="john”print(name.lower())

Output:

john

isupper():

This function will return True if all the present characters in the string are in uppercase. Else it will return False.

Example Code:

name="JOHN”print(name.isupper())

Output:

True

islower():

This function will return True if all the present characters in the string are in lowercase. Else it will return False.

Example Code:

name="john”print(name.islower())

Output:

True

lstrip():

This function will remove all the white spaces to the left side of the string.

Example Code:

name=" j ohn”print(name.lstrip())

Output:

john

rstrip():

This function will remove all the white spaces to the right side of the string.

Example Code:

name="john  “ print(name.rstrip())

Output:

john

replace():

This function will replace a particular character or string with some other character or string.

Example Code:

name="hello my name is john”print(name.replace(“john”,”smith”))

Output:

hello my name is smith

find():

This function returns the first occurrence of the specified character or substring.

Example code:

name=“hello my name is john”print(name.find(“o”))

Output:

4

Here in the above code, the character "o" occurs at index 4 for the first time.

rfind():

This function returns the last occurrence of the specified character or substring.

Example Code:

name=“hello my name is john”print(name.rfind(“o”))

Output:

18

Here in the above code, the character "o" occurs at index 18 for the last time.

count():

This function will count the number of occurrences of the specified substring or character in the string.

Example Code:

name=“hello my name is john”print(name.count(“o”))

Output:

2

isaplha():

This function returns True if all the characters in the string are alphabets. Else it returns False.

Example Code:

name=“john”print(name.isalpha())

Output:

True

Note: If white spaces are present then it will be counted as a non-alphabetic string.

isalnum():

This function returns True if the string is composed of only alphanumeric numbers. Else it returns False.

Example Code:

name=“john123"print(name.isalnum())

Output:

True

Note: If white spaces are present then it will be counted as a non-alphanumeric string.

isascii():

The function will return True if the string is entirely made up of ASCII characters. Else it returns False.

Example Code:

name="john123#$4_”print(name.isascii())

Output:

True

isdigit():

This function will return True if the string is composed of only digits. Else it will return False.

Example Code:

name=“54774"print(name.isdigit())

Output:

True

join():

The function will make an iteration of the second string and will iterate through the first string.

Example Code:

name1=“hello”name2=“123"print(name1.join(name2))

Output:

1hello2hello3

swapcase():

The function will simply swap the cases of the character. If the character is in the uppercase then it will be converted into lowercase and if it is in the lowercase then it will be converted into uppercase.

Example Code:

name=“hello WORLD”print(name.swapcase())

Output:

HELLO world

Above were the basic functions of string data type.

Valid Operations in a String:

The string allows operations like concatenation or joining of two strings. When it is multiplied by a number then it will be printed as many times as the specified number. The number should only be an integer.

Example Code:

name=“hello WORLD”print(name*3)print(name+“my name is John”)

Output:

hello WORLDhello WORLDhello WORLDhello WORLDmy name is John

Explanation:

In the above code, first, we multiplied the string by 3 and hence the string got repeated and printed thrice.

In the third line, we have concatenated the string named name with "my name is john" and hence the final string is "hello WORLDmy name is John".

And there you have it. Thank you for reading.