python tutorial

Python String

Learn more about Python, one of the world’s most versatile and popular programming languages.

Python provides a number of basic data types that can be used to store different kinds of values in a variable. Let’s explore the first of these data types: python string.


Python String Data Type

The string data type is used to store information that is a sequence of characters including alphabets, symbols, and numbers. There are multiple ways to create a string data type in Python using a single-quote , a regular quotation mark (double quotes) , or a set of three single quotes (triple quotes) ‘’’. Using a set of three single quotes is a specific way of creating a string data type that can span multiple lines and have whitespace characters preserved inside it. Let’s create some string data types in our code editor, following along with this code:

As you can see, a string can be created using single, double, or triple quotes. Even though we purposely entered some whitespace characters in the voicemail_message variable, it is preserved when we see the value stored in that variable. The preferred way of creating string variables is by using single quotes unless whitespace needs to be preserved.

Python
first_name = 'John'
last_name = 'Doe'
address = "123 street, Toronto, Ontario, Canada ABC XYZ"
voicemail_message = '''Hi you have reached John Doe.
Please leave a message and 
I will get back to you as soon as possible'''

print(first_name)
print(last_name)
print(address)
print(voicemail_message)
Output
John
Doe
123 street, Toronto, Ontario, Canada ABC XYZ
Hi you have reached John Doe. \n
Please leave a message and \n
I will get back to you as soon as possible

Python String Split

String data type in Python is very powerful for representing textual information in code. Hence, Python is designed to do all possible operations on these string values to represent them in a way that suits your needs. Python allows many different operations that could be performed on these string values and one of the most common operations is splitting the string to extract information from it.

Strings in Python can be splitted using a pre-built method called split(). By default, the method is of the structure str.split(separator, maxsplit) where str is the string to be split, separator is the delimiter or character on which the string should be split and maxsplit to define the maximum number of splits to be obtained for this string. If the separator is not specified, the string will be split on whitespace characters like spaces and tabs.

Python
sentence = "split this string on whitespace into individual words"
print(sentence.split())

todos = 'Finish Assignment,Work on Python Lab,Watch Movie,Go to Grocery Store'
# split todos on a specific character i.e. comma ,
print(todos.split(','))

# define maximum number of splits to be obtained from a string
# extract or split first todo item from rest by defining maximum number or splits
print(todos.split(',', 1))
Output
['split', 'this', 'string', 'on', 'whitespace', 'into', 'individual', 'words']
['Finish Assignment', 'Work on Python Lab', 'Watch Movie', 'Go to Grocery Store']
['Finish Assignment', 'Work on Python Lab,Watch Movie,Go to Grocery Store']

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required