Software DD (Implementation: String Handling) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What would be the output of this code?
mystring = “The quick brown fox jumps over the lazy dog”
print(mystring[2])

A

e

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What would be the output of this code?

mystring = “The quick brown fox jumps over the lazy dog”
print(mystring[0:3])

A

The

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What would be the output of this code?

original =”I am a string”
print(original[2:4])

A

am

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What would be the output of this code?

original =”I am a string”
print(original[7:len(original)])

A

string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What would be the output of this code?

original =”I am a string”
print(original[0:4])

A

I am

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What would be the output of this code?

original =”I am a string”
print(original[-4])

A

r

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What would be the output of this code?

original =”I am a string”
print(original[-6:])

A

string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What would be the output of this code?

original = “This string is my original”
print(original[0:3:2])

A

Gives us the following output - as Python will print the characters from position 0 to before position 3 but increasing the position by 2 each time. So will print out the characters at position 0 and 2.

Ti

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What would be the output of this code?

original = “This string is my original”
print(original[::-1])

A

lanigiro ym si gnirts sihT

Generic method:
original = “This string is my original”
newstring = “”
for x in range(len(original)-1,-1,-1):
newstring += original[x]
print (newstring)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Convert string into an ASCII Value

A

use ord()

Example:
character = ord(“a”)
print(character)

Output: 97

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Convert ASCII Value to a string

A

use chr()

Example:
character = chr(65)
print(character)

Output: A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Example problem from RGC Aberdeen website:

So we can use this to generate a random value between 65 and 90 and then grab the corresponding ASCII value to generate a random uppercase character such as below. This could be a useful technique for generating passwords etc.

A

import random
character = chr(random.randint(65,90))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the modulus function do?

A

The modulus function or operator gives the remainder of a division. In some languages it is accessed by a predefined function but in Python is performed using the % operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What would be the output of this code?

remainder = 10 % 3
print(“Remainder = “ + str(remainder))

A

Remainder = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does int() do?

A

My Summary: Converts a real number to an integer. Unlike round() it doesn’t round the real number, int() simply cuts the mantissa off so int(6.833333333) would equal 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly