Python Strings

python-strings-ipcisco

Python Strings are the sequence of alphabet characters. As you know in English, there are 26 letters. A Python string is created with these letters and other special characters with the help of quotation marks or double quotation marks. In Python Programming Course, we will give a lot of examples of these strings.

 

Normally computers work with binary numbers. They do not know strings. You see these strings but computer sees 1s and 0s. This conversion is called encoding. The reverse of it is decoding. ASCII and Unicode are two of the encoding mechanisms. Python uses Unicode characters for encoding. Because Unicode includes all the characters of all the languages.

 

To learn more about Python Strings, also visit the below lessons:

 



 

Python String Examples

 

So, how is a a string in Python? Below you can find an example for a Python String.

 

‘Bonjour’

 

Or

 

“Hola”

 

 

To print these strings, we can use print function.

 

print('Bonjour')
print("Hola")

 

The output will be hello word in French and in Spanish like below:

 

Bonjour
Hola

 

We can also assign a string to a variable. Below, we will assign Bonjour string to variable x.

 

x = "Bonjour"
print(x)
Bonjour

 


 

Creating Strings in Multiple Lines

 

We can also create a string that includes many lines as a paragraph. To do this we will use three double quotes (“””) or three single quotes (”’). We will write the string between three double quotes.

 

x = """Hello, How are you today?
Where were you yesterday?
Will you coming to the party?
I would like to see you there too much!"""
print(x)

 

 

The output of this Python String example will be:

 

Hello, How are you today?
Where were you yesterday?
Will you coming to the party?
I would like to see you there too much!

 

python-strings-multiple-line-ipcisco

 

x = '''Hello, How are you today?
Where were you yesterday?
Will you coming to the party?
I would like to see you there too much!'''
print(x)

 

Again, the output will be the same.

 

Hello, How are you today?
Where were you yesterday?
Will you coming to the party?
I would like to see you there too much!

 


 

Accessing String Elements

 

To access a string element, we use square brackets. The characters in a string starts with index 0 and increase as index 1, index 2 etc. So, to access any letter of a string, we will write the previous number. For example, to access character 3, we will enter 2 inside the square brackets.

 

x = "Hola"
print(x[2])

 

The output of this python programming code will be ‘l’ like below

 

l

python-string-index-ipcisco

 

Or to access the last item in a string, we can use -1 value inside the square brackets.

 

x = "Hola"
print(x[-1])
a

 

 


 

Python String Methods

 

There are different Python String Methods used for various aims. For example, we can use these methods to find the length of a string. Or we can use it convert the string to upper case. We can count any string in a string, we can divide strings etc. To understand methods of python, let’s give examples.

 


 

Finding String Length in Python

 

We can find the length of a string in python. To do this, we will use string len method. This method counts the characters of the string including special characters and spaces. It counts anything between the quotation marks or double quotation marks.

 

x = 'Hola'
print(len(x))

 

The length of this string is

 

4

 

Now, let’s write a longer string with special characters and space.

 

x = "Routers & Switches"
print(len(x))

 

The length of this string is

 

18

length-of-a-python-string-ipcisco

 


 

Converting String to the Upper Case

 

Converting a string to upper case, lower case and capitalize is done with different methods in python programming. To do this let’s give special examples.

 

Firstly, let’s capitalize all the letters in a string. Below, we will convert all the letters to upper cases.

 

msg = "welcome to python programming course!"
x = msg.upper()
print (x)

 

The output of this code will be:

 

WELCOME TO PYTHON PROGRAMMING COURSE!

 

Now, let’s write only the first letter of a word with upper cases. To do this we will use title method

 

msg = "welcome to python programming course!"
x = msg.title()
print (x)
Welcome To Python Programming Course!

 

python-title-method-ipcisco

Lastly, let’s convert upper cases to lower cases again. Here, we will sue lower method of python strings.

 

msg = "WELCOME TO PYTHON PROGRAMMING COURSE!"
x = msg.lower()
print (x)

 

welcome to python programming course!

 


 

Counting a Word in a String

 

In python programming, we can count a string in a string with count method. In other words, we can count a word in a string sentences series. Below, we will do an example to explain it better.

 

msg = "I like routers. Especially Cisco routers are perfect!"
x = msg.count("routers")
print(x)

 

Here, we are counting routers keyword. So, the output of this code will be 2. Because there are two routers in this string.

 

2

 

Or, let’s count a word in a paragraph.

 

message = '''Hello, How are you today?
Where were you yesterday?
Will you coming to the party?
I would like to see you there too much!'''
x = message.count("you")
print(x)

 

The output will be 4, because, there are four you word in this paragraph.

 

4

python-count-method-ipcisco


 

Python String Format

 

Another important point in Python Strings is string format. With this method, we can insert a string inside the specified place in the string. To do this, we use curly brackets {}, inside the string. And the place that we insert curly brackets, it puts the keyword that we use inside the parentheses.

 

Bellow, we will go to Lord of the Rings and add a word from this perfect movie.

 

msg = "Legolas is a {} character."
print(msg.format("Elf"))

 

Here, the code will insert Elf keyword, where ever it sees curly brackets {}. So, the output of this code will be like below:

 

Legolas is a Elf character.

python-string-format-ipcisco-1


 

More on Python Strings

 

In this lesson, we have learned what is a Python String. We have explained different python methods with different python string examples. To learn more about Python lessons, you can check the other detailed Python string lessons below:

 



 

 

Back to: Python Programming Course > Python Strings

Leave a Reply

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

Python Programming Course

Collapse
Expand