String Manipulation in Python Programming

The string is regarded as a text in computer programming, and it’s also a datatype in most computer programming. In most programming languages string Data type is used to hold alphanumeric characters.
In this article, we will learn how to handle string Datatype.
Below are some of the things you will learn in this article.

  1. String variable
  2. Getting to know the length of a
    string.
  3. Changing the case of a string.
  4. Concatenating two strings
    together

String Variable

Before we go into what a string variable means in python programming, we will have to know what a variable means in programming generally.
In programming, the variable is regarded as a memory location that’s used to store values that the programmer makes use of as the programming executes. When a variable is created, it reserves memory to store a value, and the process of assigning a value to a variable is called variable initialization and every variable has their different reasons why it’s created. The programmer can create a variable to store a value for later use and every programming language has their different ways of creating a variable.
In python programming, to create a variable is very simple compared to some other programming language such as C++, Vb, etc.
String Datatype variable is used to store alphanumeric characters and it’s always surrounded by double quotes.
Note: in python programming variable, Datatype is determined when a value has been assigned to it. What this means is that, when you create a variable in python, the compiler will not be able to determine its type until you have assigned a value to it, unlike other programming languages that you will need to specify the Datatype during the creation of the variable.
Let’s see how the variable is created in python programming

Example

Firstname

At this point, you just successfully created a variable but no value is assigned to it yet, this will cause an error in the program if you run it because python will try to look for the variable you are referring to.

Firstname="Liberty"

At this point we just assigned a value to the created variable, remember that a string in most programming is always surrounded by double quotes, for that reason python will regard Firstname as a string variable.
Now let’s write a python program to create a variable, initialize the variable by assigning a value to it then output it using the python print function.

#python program to print the content of a variable
Firstname="Liberty"
# printing the value of Firstname on the screen using the print function
print(firstname)

Output: Liberty

Program Explanation
In the program above, the first line of the program contains a comment explaining the function of the program. In the second line, creation and initialization of variables are done, and in the third line, a comment explaining the print function that is used to print the value of the variable on the screen. While the last line there is a print function that output the value of the variable.

Knowing The Length Of A String In Python Programming

Every programming language has its different ways of getting the length of a string. In this section of this article, I will explain with an example of how to get the length of a variable.
Getting the length of a string in python is straightforward. The length of a string is the total number of characters in the string content and it’s written as len().
Let’s rewrite the program above but this time around we are going to output the content of the Firstname and also the length.

Example

#python program to print the content of a variable
Firstname="Liberty"
# printing the value of Firstname on the screen using the print function
print(len(Firstname))
print(Firstname)

The program will produce the following result if its compiled and executed

7
Liberty

Let’s take another example but this time it’s not going to be a single word.
Example

#python program to print the content of a variable
Firstname="Liberty is back to python class"
# printing the value of Firstname on the screen using the print function
print(len(Firstname))
print(Firstname)

Output
31
Liberty is back to the python class

Changing The Case Of A String In Python Programming

Python allows you to manipulate the content of a variable, especially a string variable, with python you can change a string variable from lowercase to uppercase. In this article, we will learn how to change

Uppercase

Uppercase in python is an inbuilt function that is used to return the uppercase of a string, it converts the content of a string variable to uppercase.
Let’s write a simple program to see how it works by converting a string to uppercase

Name="liberty"
#convert to uppercase
print(Name. upper())

At the first line of the program above, we create a variable and a value is assigned to it, looking at the value of the created variable is a string, at the second line of the program the print function is used to print the content of the variable but before the content is printed out it was converted to uppercase using the dot upper() inbuilt function.

The program will produce the following result if it is compiled and executed.

LIBERTY

Lowercase

This is an inbuilt function that enables programmers to convert any given string that’s written in uppercase to lowercase, this function is the opposite of Uppercase and it’s written dotlower.
Let’s write a simple python program to see how this function work.

Name="LIBERTY"
#convert to Lowercase
print(Name. lower())

If the above program is compiled and executed, it will produce the following result.

liberty

Code explanation, Now look at the code closely, you will notice that the content of “Name” is written in uppercase but with the lower function, python convert it into Capital Letter before printing it out.

String Concatenation

String Concatenation is the process of joining two different strings together to be one. Every programming language has different methods of doing this. Python uses the following techniques for Concatenation

  1. Addition operator (+)
  2. Comma (,)

Addition operator (+)

The addition operator (+) is an arithmetic operator that is used to operate arithmetic operations on variables in python but it can also be used to join string together in python. Let’s take a look at how this function work when it comes to String Concatenation.

Name="Liberty"
print(Name+" is back to python class")

The above code produces the following result if it is compiled and executed.

Liberty is back to the python class

Now let’s try another example

a=2
b=2
c=a+b
print("The answer is " + c)

Output
Traceback (most recent call last):
  File “/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_runpy”, line 31, in <module>
    start(fakepyfile, mainly file)
  File “/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py”, line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File “<string>”, line 4, in <module>
TypeError: can only concatenate str (not “int”) to str

Code explanation

The above code will generate an error because instead of python concatenating the string with the variable, it’s trying to perform the arithmetic operation, for you to overcome such an error, it’s either the integer Datatype is converted to string Datatype or we used the second method of Concatenation. Python has an inbuilt function that enables you to convert from integer to string and its writing as str().

Now let’s rewrite the code then convert the integer value to a string and see the output

a=2
b=2
c=a+b
print("The answer is " + str(c))

program output

The answer is 4

Comma(,) method

The comma method is another way of Concatenating two variables in python programming, without wasting time let’s see how this method work.

a=2
b=2
c=a+b
print("The answer is ", c)

Output
The answer is 4

Wrapping up

In this article, I have explained to you

  1. what a string variable is programming.
  2. How to get the length of a string variable
  3. How to convert the case of a string to uppercase or lowercase
  4. String Concatenation in python programming

All the examples in this article are tested and they all working fine, if there are any questions in regards to this article, let us know in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *