Strings Flashcards
Write a brief description of strings in Python.
Strings are used to record text information. They can be used with double or single quotations and are immutable
Write a one word string
‘hello’
Write a phase in a string
"”hello world this is a string”
‘I’m using single quotes but I will still get an error’
Explain why an error will occur.
An error will ouccur because there is a single quote in What’s stopped the string. Using single and quote combinations will get the complete statement.
“I’m now able use single quotes inside a string”
How do you output multiple strings?
Use a print statement to print a string.
print(‘hello world’)
print(hello world 2’)
What function is used to check the length of a string
len()
s = ‘Hello World’
What element is:
s[0]
‘H’
s = ‘Hello World’
What element is:
s[4]
‘o’
s = ‘Hello World’
output - s[1:]
s[1:] ‘ ello World’
s = ‘Hello World’
output s[:3]
‘Hel’
s = ‘Hello World’
Grab everything
s[:]
s = ‘Hello World’
output [:-1]
‘d’
s = ‘Hello World’
Grab everything in steps size 2
s[::2] ‘HloWrd’
s = ‘Hello World’
output - s[::-1]
‘dlroW olleH’
Strings are Immutable True or False?
True
s = ‘Hello ‘World’
concatenate this string
s = s + ‘concatenate me’
print(s)
Hello World concatenate me
letter = ‘z’
letter*10 =
‘zzzzzzzzzz’
What method us used to split a string by blank space?
s.split()
What does it mean to concatenate in Python
Concatenate means to add a new string that contains the original string
‘2’ + ‘3’
output
23
x = ‘This is a string’
x.split()
output -
[‘This’, ‘is’, ‘a’, ‘string’]
x = ‘This is a string’
x.split(‘s’)
output -
[‘Thi’, ‘i’, ‘a’ ‘tring’]
mystring = ‘hello world’
mystring[-3]
output -
‘r’
mystring = ‘fabrication’
mystring[2:]
output -
‘brication’
mystring = ‘tomatoes’
mystring[:3]
‘tom’
mystring = ‘trainers’
mystring[3:6]
‘ine’
mystring= ‘fabrication’
mystring[::]
‘fabrication’
mystring= ‘quantifiers’
mystring[::5]
‘qis’
mystring = ‘abcdefghijk’
mystring[2:7:2]
‘ceg’