0% found this document useful (0 votes)
9 views19 pages

14 - String

The document explains the string data type in Python, highlighting its immutability and ordered nature. It covers how to define strings using single or double quotations, the importance of escape characters, and basic string operations such as concatenation, repetition, and length calculation. Additionally, it discusses taking user input with the input() function and how to cast the input to different data types.

Uploaded by

linghao0521
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

14 - String

The document explains the string data type in Python, highlighting its immutability and ordered nature. It covers how to define strings using single or double quotations, the importance of escape characters, and basic string operations such as concatenation, repetition, and length calculation. Additionally, it discusses taking user input with the input() function and how to cast the input to different data types.

Uploaded by

linghao0521
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

String in Python

String
So far we've dealt with numbers and booleans. But there is a very powerful data type
in python, which is the string. String is an immutable ordered data type that stores
sequences of characters. We will discuss later what immutable and ordered mean.

Remember the first python code we wrote earlier?


print("Hello World!")
This is actually an example of a string. We define a string by the double quotation, and
then we can write between these quotations anything we need: characters - numbers -
symbols - spaces .. etc
String
We can also use single quotations when dealing with strings.

Both of these work the same, and both of them define a string.
String
However, there are some cases where we should use single quotations and some
other cases where we should use double quotations.
String
For example:
Imagine we are trying to write the following:

You're really a great programmer

When using single quotations, this will cause an error, this is invalid in python.
String
There are two solutions to this problem:

1. Use double quotations instead of single quotations

2. Use a backslash \ before the quotation (aka escape character)


String
1. Use double quotations instead of single quotations

As you see, this works fine after replacing ' with ".
String
2. Use a backslash \ before the quotation (aka escape character)

Escape sequences are special characters that allow you to include special characters

in strings.
String Operations
1. Combine (Concatenation)

2. String Repeat

3. len
String Operations
1. Combine (Concatenation)

Imagine we have these two strings:

Hello

World!

We can use the + operator to combine(concatenate) the two strings into one string.
String Operations
1. Combine (Concatenation)

Similar to the + operator with numbers, the + operator with strings allows us to

combine two strings together, to have a new string which is the concatenation of

these two strings.


String Operations
2. Repeat

When using the * operator with strings, we basically repeat the string as much as we

want. In the example below we repeated the string "Hello" 5 times, which resulted in

the string shown below.


String Operations
Python is very accurate with concatenation and repeating, let's see an example:

Notice the difference in the above pictures. Adding an extra space changed the output

completely, so be accurate when dealing with string concatenation or repeating,

because every punctuation or space matters.


String Operations
3. len

We can use the len() function to get the length/size of the string.

"Omar" consists of 4 characters, therefore the length of it is 4 characters


String Operations
3. len

Consider the following example:

Although there are only 10 characters in the string, the output is 11. Why?

This is because the space is between "Omar" and "Marouf" is also considered a

character, therefore there are 11 characters instead of 10


input()
In python, you can take input from the user using the input() function

By using the input() keyword, you can allow the user to type on the keyboard and then

take this input and do whatever you want


input()
Let's take this simple example. We want to write a program, that asks the user for

his/her name and welcomes him/her.


input()
Now what does this program do?

This program creates a string variable called name and then assigns the value of this

input to whatever the user enters, and then concatenates "Hello " to the name.

By default, input() is of type string, but we can change (cast) the string into any other

data type, like int or float for example.


input()
In this example, we took input from the user, and then cast it into float, and printed
the salary, and the type of the salary variable (which is now float)

You might also like