In: Computer Science
What is an escape sequence? Give an example in Python, and describe what it does.
What Python function is used to determine the length of Python objects such as lists, tuples and string literals?
For the string literal “Bjorggson”, show by Python code example how to obtain the fourth character of that string.
Show by code example in Python how to concatenate two strings and assign the result to a variable.
Declare a Python list named famfri with the first names of five friends or family members.
Lists and tuples are examples of what kind of Python construct?
Write a Python for loop that prints the names of the
friends or family, one per line, without referencing a range or any
literal numeric values.
1. Escape Sequence : In python , Escape sequences art used to represent special set of characters.For example : \n is used to get to the next line.
Consider the code :
print("Hello\nWorld")
output :
Hello
World
2. len() function is an inbuild function in Python which is used to determine the length of the python objects such as lists and strings.
Consider the following Code :
print(len("Hello")) #len function used to determine the length of string hello
Output :
5
3. Consider the following code :
StringName = 'Bjorggson'
print(StringName[3])
The above code is used to get the fourth character in the string.
Output :
r
4. consider the following code :
a = 'Hello' #created first string
b = 'World' #created second string
c = a + b #concatenated two strings and assigned to a
variable.
print(c)
Output :
HelloWorld
5. consider the following code :
famfri = ['A', 'B', 'C', 'D' , 'E'] # list with name famfri is
created for storing first name of five friends or family
members.
print(famfri)
Output :
['A', 'B', 'C', 'D' , 'E']
6. consider the following code to print family name line by line using for loop without range :
famfri = ['A', 'B', 'C', 'D' , 'E']
for names in famfri :
print(*famfri, sep = '\n')
break;
Output :
A
B
C
D
E