[Python] String Data Type

Bishal Shrestha
6 min readJun 2, 2021
Python, Courtesy: DigitalOcean

String is one of the most common data type of python along with Numbers, Boolean, etc.

String literals can be enclosed by a single or double quotes.
For example,

str1 = 'I love the way you lie.'
str2 = "I love the way you lie."

Here, both str1 & str2 is correct way for representing a string.

Some important notes regarding indexing of python strings:

  1. Python string are indexed from 0, not 1.
    i.e name = “Bishal”
    Here, name[0] = “B” , name[1] = “i” and so on.
  2. Indexing from the rightmost side is allowed and starts at -1, not 0.
    i.e name = “Bishal”
    Here, name[-1] = “l” , name[-2] = “a” and so on.

Accessing the Python String

In python, we can access/print a whole string or a part of a string, also called as substring.

For example,

text = "Solar System!"print(text) 
# Outputs whole string
# Output : Solar System!
print(text[:])
# Outputs whole string
# Output : Solar System!
print(text[0])
# Outputs a substring with index 0
# Output : S
print(text[3])
# Outputs a substring with index 3
# Output : a
print(text[3:])
# Outputs a substring from index 3 till the end
# Output : ar System!
print(text[3:5])
# Outputs a substring from index 3 to 4 (not till index 5)
# Output : ar
print(text[:4])
# Outputs a substring from index 0 till index 3
# Output : Sola

Length of a String

Length of a string can be accessed using a built in string class len().

text = "Solar System!"print(len(text))
# Output : 13

Escape Sequence In String

The backslash (\) character is used to escape characters.

Escape characters are very important in most programming paradigm. Some of the important and mostly used escape characters are shown in the code below:

print("Name \t Bishal")
# Output : Name Bishal
print("This is a first line \nThis is a second line")
# Output : This is a first line
# This is a second line
print("John said to Karen,\"How was your day?\"")
# Output : John said to Karen,"How was your day?"

These are some commonly used escape characters in Python. For full list, visit here.

Concatenation of Python Strings

Basically, concatenation (the act of linking things together in a series) is simply joining two strings in a series fashion.

Let me explain,

first_name = "Bishal"
last_name = "Shrestha"
print("My name is " + first_name + " " + last_name + ".")
# Output : My name is Bishal Shrestha

Important point to be noted:
Concatenation is only allowed between any two strings, but not with other data types like numbers & booleans.
For example,

name = "Bishal Shrestha" # This is a string
age = 24 # This is an integer
print("My name is " + name + " and my age is " + age)
# This shows some error like this,
Traceback (most recent call last):
File “C:\Users\iambs\PycharmProjects\firstpythonproject\main.py”, line 3, in <module>
print(“My name is “ + name + “ and my age is “ + age)
TypeError: can only concatenate str (not “int”) to str

You can see the last line of the error, as it says, TypeError: can only concatenate str (not “int”) to str — i.e. concatenation is possible only if the joining data types are strings.

So how are we gonna get around this?

Simply, converting the given integer data into string using str() function.

str() is a built in string class of python that converts anything placed inside the () into a string.

name = "Bishal Shrestha" # This is a string
age = 24 # This is an integer
print("My name is " + name + " and my age is " + str(age))
# Output : My name is Bishal Shrestha and my age is 24

Saw the difference ? See the last line and compare it with the original code.

Updating Python Strings

Updating a whole string is allowed in python but updating a part of a string is not allowed. But don’t worry, there is a somewhat odd way of updating a substring(part of a string).

# Updating whole strings

# First string
text = "Solar System!"
print(text)
# Output : Solar System!

# Changed String
text = "Galaxy!"
print(text)
# Output : Galaxy!

But to change a substring, we can slice a substrings and then concatenate the substrings together along with the updated substring. Let me explain this with an example,

# Updating substring

text = "Soler System!"

text_before = text[:3]
# Equals - Sol

text_after = text[4:]
# Equals = r System!

insert_in_between = "a"

updated_text = text_before + insert_in_between + text_after
print(updated_text)
# Output : Solar System!

What we did here is that we sliced a string into parts (“Sol” and “r System!”) and then appended them together along with the update. Python actually does not allow us to update a substring but this is a way around for updating a substring. This might be helpful in some cases.

Python String Methods

Python supports numerous string manipulating methods. Some of the most common usage are shown below:

# Common String Manipulating Methods

text = "SolAR sYstEm!"

print(text.lower())
# Output : solar system!

print(text.upper())
# Output : SOLAR SYSTEM!

print(text.capitalize())
# Output : Solar System!

print(text.find("o"))
# Output : 1
# This is used to determine the index of a substring
# Is case sensitive
# Returns -1 if not found

print(text.casefold())
# Output : solar system!
# Casefolding is similar to lowercasing but more aggressive
# because it is intended to remove all case distinctions in a string.
# For example, the German lowercase letter 'ß' is equivalent to "ss".
# Since it is already lowercase, lower() would do nothing to 'ß'; casefold() converts it to "ss".

str.format(*args,*kwargs) method

This method is pretty useful in some cases. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.

# Use of format() method

print("Hi! My name is {} and I am {} years old".format("Bishal",24))
# Output : My name is Bishal and I am 24 years old
# Values are placed in sequence
print("Hi! My name is {name} and I am {age} years old".format(name = "Bishal",age = 24))
# Output : My name is Bishal and I am 24 years old
# Values can be passed as dictionary

print("Hi! My name is {} and I am {} years old".format("Bishal", 2021 - 1997))
# Output : My name is Bishal and I am 24 years old
# Operators are allowed while passing the arguments

str.format_map(mapping) method

Similar to str.format(**mapping), except that mapping is used directly and not copied to a dict. This is useful if for example mapping is a dict subclass:

#Use of fromat_map(mapping)
class Default(dict):
def __missing__(self, key):
return key

print("My name is {name} and I am {age} years old. I live in {address}".format_map(Default(name="Bishal",age=24)))
Output : My name is Bishal and I am 24 years old. I live in address

split() method

This method is helpful in converting a data separated by a delimiter/comma into a list.

names = "Harry, Meghan, John, Albert"

print(names.split())
# Output : ['Harry,', 'Meghan,', 'John,', 'Albert']

join(iterables) method

The join() string method returns a string by joining all the elements of an iterable, separated by a string separator.

The join() method takes an iterables(ex. List, Tuple, String, Dictionary, Set, File Objects, etc. ) as its parameters.

Usage with list

print(" and ".join(["John","Harry","Meghan"]))
# Output : John and Harry and Meghan

Usage with tuple

print(" and ".join(("1","2")))
# Output : 1 and 2

Usage with string

print(", ".join("123"))
# Output : 1, 2, 3

Usage with set

print(" and ".join({'1','2','3'}))
# Output : 1 and 2 and 3

Usage with dictionary

While using join() method with dictionary, only key from the key-value pairs is joined.

obj = {'key': 'value','Bishal': 'name','Shrestha': 'surname'}
print(", ".join(obj))
# Output : key, Bishal, Shrestha

The key is necessary to be only string type. If the key is not a string, it passes a Traceback error.

obj = { 1 : 'value','Bishal': 'name','Shrestha': 'surname'}
print(", ".join(obj))
# Traceback (most recent call last):
# File"C:\Users\iambs\PycharmProjects\firstpythonproject\main.py", line 23, in <module>
# print(", ".join(obj))
# TypeError: sequence item 0: expected str instance, int found

There are numerous other useful methods in python. For full list, visit here.

This post will be updated regularly. Feel free to connect with me on Facebook.

--

--