Search Header Logo
Strings in Python

Strings in Python

Assessment

Presentation

Computers

8th Grade

Practice Problem

Medium

Created by

Klea h

Used 3+ times

FREE Resource

9 Slides • 12 Questions

1

PYTHON STRINGS

By Klea h

2

Strings in python are surrounded by either single quotation marks, or double quotation marks.

'hello' is the same as "hello".

You can assign a multiline string to a variable by using TRIPLE quotes.

''' Lorem ipsum dolor sit amet,

ut labore et dolore magna aliqua.'''

media

3

Multiple Choice

Which of the following is a valid way to define a string in Python?

1

greeting = 'Hi there'

2

greeting = "Hi there"

3

greeting = '''Hi there'''

4

All of the above

4

Multiple Choice

What is the correct way to create a multiline string in Python?


B) note = '''Line1 Line2 Line3'''
C)
D) note = "Line1 Line2 Line3"

1

note = '''Line1

Line2

Line3'''

2

note = "Line1

Line2

Line3"

3

note = 'Line1

Line2

Line3'

4

I have no Idea

5

​Square brackets can be used to access elements of the string.

The first character in Python has the position 0


a = "PYTHON"
print(a[1])


RESULT: Y

media

6

Multiple Choice

What will this print?

word = "Computer"

print(word[3])

1

p

2
o
3

u

4

C

7

Multiple Choice

What is the index of the first character in a string?

1
1
2
-1
3

It depends on the user

4
0

8

​To get the length of a string, use the len() function.

a = "Hello, World!"

print(len(a))

RESULT: 13

media

9

Multiple Choice

Which function is used to find how many characters a string has?

1

length

2

len()

3

length()

4

len

10

Multiple Choice

What is the output of the following code?

msg = "Good morning"

print(len(msg))

1
12
2
10
3
15
4

error

11

Check String

To check if a certain phrase or character is present in a string, we use the keyword in.

txt = "The best things in life are free!"

print("free" in txt)
RESULT: True

To check if a certain phrase or character is NOT present in a string, we can use the keyword not in.

txt = "The best things in life are free!"

print("expensive" not in txt)
RESULT: True

12

Multiple Choice

What does the following code return?

sentence = "Coding is fun!"

print("fun" in sentence)

1
True
2
False
3
None
4
Error

13

Multiple Choice

What will this return?

text = "Apples are red"

print("green" not in text)

1
Error
2
True
3
None
4
False

14

​Slicing

You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return a part of the string.

b = "Computer Lab"
print(b[2:5])

RESULT: mpu

15

​Slice From the Start

By leaving out the start index, the range will start at the first character:
b = "Hello, World!"
print(b[:5]) -> character in index 5 is NOT included




Slice To the End

By leaving out the end index, the range will go to the end:
b = "Hello, World!"
print(b[2:]) --> starting from the character in index 2 until the end

16

​Negative Indexing


Use negative indexes to start the slice from the end of the string:
From: "Y" in "World!" (position -5)
To, but not included: "O" in "World!" (position -2):

b = "PYTHON"

print(b[-5:-2])

media

17

Fill in the Blanks

18

Fill in the Blanks

19

​Python has a set of built-in methods that you can use on strings.
Note: All string methods return new values.
They do not change the original string.


The upper() method returns the string in upper case:
print(a.upper())

The lower() method returns the string in lower case:
print(a.lower())

The strip() method removes any whitespace from the beginning or the end:
print(a.strip())
# returns "Hello, World!"

20

Fill in the Blanks

21

Fill in the Blanks

PYTHON STRINGS

By Klea h

Show answer

Auto Play

Slide 1 / 21

SLIDE