Strings are most used datastructure in Python. It is actually used to store text information.
Strings are actaully sequence in Python,each character in the String is stored in specific order.
We can use various String’s inbuilt methods to perform different operations.
Python will understand String “Code” in sequence of characters in specific orders.
In this lecture we’ll learn about the following:
Table of Contents
Creating a String
To create a string in Python you need to use either single quotes or double quotes. For example:
# Single word
str = 'code'
# Entire sentence
str='Welcome to code2master'
# We can also use double quote
str="Welcome to code2master in double quotes"
Now let’s learn about printing strings!
Printing a String
You can easily print String using print function.
# We can simply declare a string
str="Welcome to code2master"
We can use a print function to print a string as seen above.
As strings are a sequence, so Python can use indexes to get parts of the sequence. Let’s see how can use indexing.
In Python, you use brackets  after an object to get its index.Please note that indexing starts at 0 for Python. Let’s create a new object called s and the walk through a few examples of indexing.
# Assign s as a string s = 'Hello World' # Print the object print(s) # Show first element (in this case a letter) print(s) print(s) print(s) print(s) print(s)
We can use “:” to perform slicing which grabs everything up to a designated point. For example:
s='Code2Master' print(s[1:]) print(s[3:]) print(s[1:3])
Note the above slicing. Here we’re telling Python to get everything from 1 up to 3 but it does not include 3.
We can also use negative indexing to go backwards.
s='Hello' #Everything print(s[:]) # Last letter (one index behind 0 so it loops back around) print(s[:-1])
We can also use index and slice notation to get elements of a sequence by a step size (the default is 1). For instance we can use two colons in a row and then a number specifying the frequency to grab elements.
s='Hello World' # Grab everything, but go in steps size of 1 print(s[::1]) # Grab everything, but go in step sizes of 2 print(s[::2]) # We can use this to print a string backwards print(s[::-1])
Its important to note that strings have an important property known as immutability. This means that once a string is created, the elements within it can not be changed or replaced. For example:
s # Let's try to change the first letter to 'x' s = 'x'
TypeError Traceback (most recent call last)
1 # Let’s try to change the first letter to ‘x’
—-> 2 s = ‘x’
TypeError: ‘str’ object does not support item assignment
Notice how the error tells us directly what we can’t do, change the item assignment!
s='Hello' # Concatenate strings! s + ' world!' print(s) # We can reassign s completely though! s =s + ' world!' print(s)
We can use the multiplication symbol to create repetition!
letter = 'a' print(letter*5)
Basic Built-in String methods
Objects in Python usually have built-in methods. These methods are functions inside the object (we will learn about these in much more depth later) that can perform actions or commands on the object itself.
We call methods with a period and then the method name. Methods are in the form:
Where parameters are extra arguments we can pass into the method. Don’t worry if the details don’t make 100% sense right now. Later on we will be creating our own objects and functions!
Here are some examples of built-in methods in strings:
s='Welcome to code2master' # Upper Case a string print(s.upper()) # Lower case print(s.lower()) # Split a string by blank space (this is the default) print(s.split()) # Split by a specific element (doesn't include the element that was split on) print(s.split('c'))
welcome to code2master
[‘Welcome’, ‘to’, ‘code2master’]
[‘Wel’, ‘ome to ‘, ‘ode2master’]
We can use the .format() method to add formatted objects to printed string statements.
The easiest way to show this is through an example:
That’s all about Strings in Python.