Python Strings

Python Strings are the most popular data type in Python. Remember string is not popular data type of Python but it’s also very popular among other programming languages like Java, JavaScript etc. In Python Strings are represented by the shortcut “str”.

What is String Data Type in Python?

A string is simply a piece of text and it is written in double or single quotes in Python.

For Example:

You can write string in Python like this:

“This is a string” or ‘This is a string’ or ‘78’

In python both are valid because one is written in double quote and one is written in single quote. You can put anything in strings like numbers , ! Marks and it’s just a piece of text. You can check the type of string or any data type in python by the “type” function.

print(type(“This is a string”))
#The Output will be:
class, ‘str’

In the above example we can see the console display its type because we printed the type of a string with the print function.

Writing Long Strings in Python

You can also write long strings in python by using the three single quotes ”’ at the start of sting and three at the end of string. Inside these quotes we can write our long strings like:

long_sting = '''
Wow! 
This is a long String. 
'''
print(long_string)

You can copy this code and run into the python interpreter.

So, the three single quotes ”’ are for long strings. In python you can not create long strings with double ” ” and ‘ ‘ single quotes. If create long strings with single and double quotes you will get an error.

Read More Python Data Types

String Concatenation

Imagine you are creating a login form and have two fields First name and Fast name. You want to combine them to get Full name then you can do this by string concatenation.

firstname = 'Sial'
lastname = 'Jutt'
full_name = firstname + lastname
print(full_name)

Here we declared two variables firstname and lastname and assign them values with First name of the user and Last name of the user. Remember the values of these variables are strings because we used quotes around them. We can concatenate these strings by using the plus (+) operator. Next we have create a new variable full_name and added firstname and lastname together by using the plus (+) operator and at last we printed the variable full_name to see the result. Run this code in the python interpreter to get see the result of this concatenation of two strings.

Type conversion in Python

Type conversion in Python is used to convert numbers, or other types into the string data type. You can use type conversion like this:

str(100)
#This will convert the number into a string data type. 
print(type(str(100)))
#If you print this to the console you will get
<class, 'str'>

First it will evaluate this code “str(100)”. This code will convert 100 to a string by using “str” action and then it will check its type because we have used the “type” function before the “str(100)” and then it will print the result to the console.

You can also convert a string to integer with type conversion in Python. This will use the same Scenario but we have to use “int” action instead of “str”.

print(type(int(str(100))))
#This will convert and string into an integer. 

Here first we converted 100 into a string and then a string into an integer by using the “int” action. The type conversion is very useful in Python when you want to convert type of a data type.

If you like this article about Python Strings. Please share it with your fiends on Facebook, and Twitter etc.

Muzammal Ahmad

I am an Ethical Hacker, Programmer, and SEO (Search Engine Optimization) expert. I love to write blogs and share knowledge with people. I also like to help people to solve their technical or internet relates problems.

Leave a Reply

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