Home » Python » Python Data Types

What is a String in Python

A string in python is a single character or combination of different types of characters, i.e., alphabets, numbers and symbols. For example: "I have 10 mangoes.", "Hello World!", and so on. Even a single alphabetic character is also a string, like 'a' and 's'.

Don't get confuse between char(not in python) and string data-type. Python does not support char, which is used in other languages for single character data. Instead it uses string data-type for single characters too.

In this tutorial we will learn to create single and multiple line strings, manipulating string data, and display string data. But, do you know how our system reads string data?

Our computer only understands binary data and nothing else. All the characters present in your keyboard has a specified encoded value that computer understands. These encoded values are called ASCII or Unicode values. So, your smart Python interpreter will convert these strings into their encoded versions to store them in the memory. For example: A's ASCII code is 65, whose binary is 01000001.

Defining String in Python?

String are defined in python by assigning a value enclosed between quotes, for ex. "Hello!". These quotes can be single, double or triple based on the situation.

First let's talk about defining string using single and double quotes. These quotes works similarly, for example, "Hello" and 'Hello' are same strings. But the need of using different quotes arises when there are quotes already present in the string itself.

Consider a string - He said, "My name is Jack.". Now, if we want to store this in a string variable, we have to use single quotes. For example: example='He said, "My name is Jack."'

If we use double quotes in this example like, example="He said, "My name is Jack."", the interpretor will throw a syntax error. Because it will consider the first quote as starting of string and the second one as the end. So, always use different quote other than the one present in the string.

The example would also be valid with double qoutes if we use single quotes inside. For example: example="He said, 'My name is Jack.'"

Example to create String in Python

Example
string_one = "This is a string" print(string_one) string_two = 'This is also a string' print(string_two)

Output

This is a string
This is also a string

Defining Multi-line String in python

A string enclosed within triple quotes(''' ''') is a multiple line string. It means that, interpreter will preserve line breaks by enter key.

In a normal string enclosed by single or double quotes, if you hit enter and continue writing in the next line, that will not create a line break. The line break will be omitted by the interpreter and it will display a single line of string.

So, if you want to preserve line breaks in the string then use triple quotes. Look at the example below to learn how to create multi-line string


Accessing substring in String

Accessing String values in python are too much fun. It has many different ways to print a string. The print function in oython prints a string's value.

You can print the whole string, a part of string, a single character of string or concatination of two strings. As I have told you earlier that python does not support character data-type. the single character is treated as a string of length one. These are also called substrings.

To access substring in a string you have to use square brackets. For example: print(name[5]), it will print only the 5th character of the string 'name'. Look at the example below to see some more ways to print a substring.

Example of accessing substring
a = 'hello Coderepublics' #string a
b = ' Hello Python' #string b
print (a[0:2]) #printing first two character using slice operator
print (a[4]) #printing 4th character of the string
print (a*2) #printing the string twice
print (a + b) #printing the concatenation of a and b

Output

he
o
hello Coderepublicshello Coderepublics
hello Coderepublics Hello Python

Update Strings in Python

Python strings are immutable in nature. But you can update the values in strings. to know more about this, read mutable and immutable data types in python.

Strings can be updated by assigning new string value to the existing variable. Have a look at the example below:

Example
x = "Hello World!" print(x[:6]) print(x[0:6] + "Python")

Output

Hello
Hello Python

How to change or delete a string?

Strings are immutable. This means that elements of a string cannot be changed once they have been assigned. We can simply reassign different strings to the same name.

Example
my_string = 'Hello Java' my_string = 'Hello Python' print(my_string)

Output

Hello Python


Looping Through a String in Python

Strings are like arrays; several characters join together to sorm a string. It can be looped just like arrays or lists using for loop. Looping will be introduced later in the tutorial, till then just look at the example and try to understand it.

Example
for x in "PYTHON": print(x)

Output

P
Y
T
H
O
N