How to Create String Variable in Python With Examples

In this tutorial, learn how to create string variable in Python. You can create a string in Python and access its characters. Find the length and join two or more strings in Python with the examples given here.

You can assign a string value enclosed within single quotes or double quotes. Learn each section of the post with working examples.

What is a String Variable?

A string is a sequence of characters enclosed within the single or double quotes. Many programming languages have a character data type. However, in Python, strings are the sequence of unicode characters.

Python does not have character data type to store the string. It has a string as a data type to store a sequence of characters in a variable.

Create String Variable

To create a string variable in Python, you have to add a sequence of character within single or double quotes.

The only difference between the single quote and the double quote string is. You cannot add any single quotes character(like it’s) in the single quotes string. But in double quote string, you can add single quoted character string.

The above example contains the two string. The string first enclosed within the double quotes. You can add single quoted character in the first string. The second string enclosed within the single quote where you cannot add any single quote character.

How to Get String Characters Using Python?

To get the string character and print the output in Python. You have to use the method of indexing and slicing.

To get All the Characters in the Output
To get all the character of the string, you have to just put the string variable in print function.

Output

How are you today?

The above example prints all the string characters in the output. But, inorder to get a single character and the requested character of the string. You have to use the indexing in Python.

get Only the Requested Character in the Output

The indexing gives the character using the square bracket([]) which starts from zero(0). See the example below to print only the character you want in the output.

Output

H
w

The above example print only the mentioned characte in the index. The first output prints the first character and the second output prints the third character.

One more thing you should note here if you give 3 as the index. You will get no string in the output. This is because the third string is a space and the output gives a space.

To get All the Characters Within the Range in the Output

However, to get the range of character, you have to use the slicing. The slicing in Python can be used by using the colon(:) within the square bracket([]). The number before the colon(:) is the start range of character. While the number after the colon(:) is the end range. You will get the character given before the end range in the output.

Output

ow
How a

The above example prints the sequence of string character in the given range. It also counts space as the character of a string if it arises within the range.

If you want to remove the space from the string, you have to read further.

Remove Whitespaces From String in Python

After you learn how to create string and access using Python. You want to learn to remove whitespaces from the string. Well, there are two functions strip() and replace() you can use to perform this task. Let’s see which function you can use to perform a suitable task.

Remove Space From the Start and the End
To remove the white space from the start and the end of the string. You have to use the Python strip() function with the string variable. Use the below-given example to perform this task.

Output

Hello World!

The above example gives output after it removes the spaces from the start and end. However, you can remove all the spaces from the string. To remove all the space, you have to read further.

How to Remove All the Spaces From the String
To remove all the white space from the string in Python. You have to use the Python replace() with the string variable. Use the below-given syntax to remove all space from your string.

Output

HelloWorld!

The above example gives the string in output without any spaces.

Find the Length Of String in Python

To find the length of the string, you have to use the Python len(). Pass the string variable as the argument of the len function. Now, if you want to print the length in the output.

You have to use the print function inside which, you have to pass the len function with the argument. Use the example given below to find the length of your string.

Output

14

The above example prints the length of the string in the output. The length of the string is 14 for the given string including the white spaces. Use this example to find the length of your string in Python programming.

Concatenate Two Strings in Python

In addition to above all examples, you can also combine or concatenate two string in Python. To combine the two string, you have to use the plus(+) operator in between the variables. Use the below example to combine your own string in the output.

Output

Hello World!How are you?

The above output contains the string after concatenation in Python. To combine your own string and print it in the output. You also have to use the Python print function to get the output of concatenation.

You may also like to read

Hope, you like this tutorial of how to create string variable in Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to create your string variable in Python.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.