Python String

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.
For example:
Python will understand String “Code” in sequence of characters in specific orders.

In this lecture we’ll learn about the following:


Creating a String

To create a string in Python you need to use either single quotes or double quotes. For example:

Now let’s learn about printing strings!


Printing a String

You can easily print String using print function.

We can use a print function to print a string as seen above.


String Indexing

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[0]) print(s[1]) print(s[2]) print(s[3]) print(s[4])

Output:

Hello World
H
e
l
l
o

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])

Output:

ode2Master
e2Master
od

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])

Output:

Hello
Hell

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.
For example:

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])

Output:

Hello World
HloWrd
dlroW olleH

String Properties

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[0] = 'x'

Output:

—————————————————————————
TypeError Traceback (most recent call last)
in ()
1 # Let’s try to change the first letter to ‘x’
—-> 2 s[0] = ‘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!


Concatenate strings

s='Hello' # Concatenate strings! s + ' world!' print(s) # We can reassign s completely though! s =s + ' world!' print(s)

Output:

Hello
Hello world!

We can use the multiplication symbol to create repetition!

letter = 'a' print(letter*5)

Output:

aaaaa

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:

object.method(parameters)

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'))

Output:

WELCOME TO CODE2MASTER
welcome to code2master
[‘Welcome’, ‘to’, ‘code2master’]
[‘Wel’, ‘ome to ‘, ‘ode2master’]

Print Formatting

We can use the .format() method to add formatted objects to printed string statements.

The easiest way to show this is through an example:

print('Hello: {}'.format('john'))

Output:

Hello: john

That’s all about Strings in Python.