"
This article is part of in the series
Last Updated: Wednesday 29th December 2021

The string is a Python data type. In this article, we will know what is the string in python, How to reverse, concatenate, and compare the string in Python. To get the most out of this article, you need to have the basic knowledge of Python. If you don't check our intro to python here. Let's start with what is the string in python.

What is the String in Python?

The Python string is a data type. Python doesn't have a character data type. That's why it has a string it's a list of characters in Unicode representation. Python deals with string as an array of characters.

This means that you can apply all list functions to string too. Declaring a python string is very easy you can simply add the word between single or double-quotes. This will tell Python to create a list in the memory with the string length. Let's see an example of defining a Python string.

text = 'Hello'
message = "Python string"

print(text)
print(message)

# output
'Hello'
"Python string"

The above example shows how you can define a string in python with single or double-quotes. The Python string has some functions that make use of the string. Let's say that users registered to your application with their email. You need to validate that this the email string is lowercase. To avoid duplication in the user's data. Python provides a built-in function lower() that convert string to lowercase.

email = "[email protected]"

print(email)
# output
'[email protected]'

print(email.lower())

# output
'[email protected]'

We mentioned that string is a list and we can execute some of the list functions to it.  This includes the reverse functionality that the list had.

How to Reverse a String in Python

To reverse a string in python you need to understand that python read the string as a list of characters. The functionality of the reverse is pretty simple. First, iterate through the string. Create a new string with staring at the end of the original string. You will understand with an example.

text = 'Hello world'
new_text = ''

index = len(text)
while index > 0:
  new_text += text[index -1]
  index -= 1

print(text)
print(new_text)

# output
Hellow world
dlrow olleH

In the above code, we created an index with the length of the string. Use the loop to go through each item in the text from back to front. The final part is to add the characters to the new string and decrease the counter. This solved our problem to reverse the string. Python supports a more efficient way to reverse the string.

String Indexing

text = 'Hello world'
new_text = text[::-1]

print(new_text)

# output
dlrow olleH

What the text[::-1] code did? It slices the string to make it starts with the length of the string and ends with a zero index. This reversed the string and stored it in the new_text string. You might have noticed in the previous example that we created an empty string and starts adding to it. This leads us to the question How to concatenate Python's string?

Concatenate String in Python

Concatenation Python's string is merging strings together. In a more simple explanation, if you have text_1 = "Hello" and text_2 = "world". concatenating them will be my merging them to formulate the "Hello world". There are many ways to concatenate strings in Python. The simplest one is using the plus signs.

text_1 = "Hello"
text_2 = "World"

print(text_1 + " " + text_2)

# output
Hello World

The more your codebase gets bigger the more you need to customize the concatenation. Python provides a couple of functions to help you concatenate the string in a more elegant and readable way. The first method is the format() function.

username = "John"

welcome_message = "Welcome {username} this is a test text".format(username=username)

print(welcome_message)

# output
Welcome John this is a test text

The format function is very handy in the large test if you needed to add spaces and you have multiple variables to add inside the text. All you need to do is to put the variable name between curly brackets. Python will get the value of this variable from the parameters passed to the format function.

The second method is using the join function.  The join function is useful if you have a list and you want to concatenate the list data to a string. Let's take an example to understand.

full_name_parts = ['John', 'Jack', 'Watson']
full_name = "-"

full_name.join(full_name_parts)

print(full_name)

# output
John-Jack-Watson

You Can see that the join function iterated through the list and added the items to the full_name string. It added the items with a -  between them.

The third method it's only available in python in Python 3.6 and above. It's the f-string method. It's a more improved version of the format function.

name = "Jack"
age = 72

f"Hi, {name}. You is {age}."

# output
'Hello, Eric. You are 74.'

The f-string method allows you to directly get the variable value in the string. If you added the f a character at the start of the string.

Compare Two Python Strings

Let's assume that you have an application that stores the username. You want the username of each user to be unique. This means that you need to validate that this name is not matching any of the other registered usernames. Python provides The ability to compare strings. A basic example of this:

name = input('Enter Your Name:')

saved_name = 'Jack'

if name == saved_name:
  print('Sorry the name you entered is taken')
else:
  print('Welcome {name}'.format(name=name))


# output
Sorry the name you entered is taken

# output
Welcome John

Python compares the two string as text. This means that if the user enters a lowercase Jack name the condition will not be valid. This is very important. You need to know that Python doesn't do automatic validation. You need to implement the validations you need manually. This was a very simple example in a more realistic example you will have a database that makes sure that the name is unique.

Conclusion

Python's string is very handy and useful to work with. It has clear formating and contamination methods. You can work with it as a list. it supports different most of the Python list built-in functions. To reverse a Python's string you can slice it and end with index 0. Concatenate strings in  Python provides different concatenation methods to choose from based on your need. Comparing strings in python are similar to compare two integers. You can know more about Python strings in the official documentation. You can get many more details about The string functions here.