Strings, String and List Methods, Exceptions Flashcards
What is the most widely used code to turn characters into numbers?
ASCII short for American Standard Code for Information Interchange
What does I18N stand for?
Internationalisation
What is a code point?
A code point is a number which makes a character. For example, 32 is a code point which makes a space in ASCII encoding. We can say that standard ASCII code consists of 128 code points.
What is a code page?
A code page is a standard for using the upper 128 code points to store specific national characters. For example, there are different code pages for Western Europe and Eastern Europe, Cyrillic and Greek alphabets, Arabic and Hebrew languages, and so on.
What does unicode do?
Unicode assigns unique (unambiguous) characters (letters, hyphens, ideograms, etc.) to more than a million code points.
The first 128 Unicode code points are identical to ASCII, and the first 256 Unicode code points are identical to the ISO/IEC 8859-1 code page (a code page designed for western European languages).
What does UCS-4 do?
UCS-4.
The name comes from Universal Character Set.
UCS-4 uses 32 bits (four bytes) to store each character, and the code is just the Unicode code points’ unique number. A file containing UCS-4 encoded text may start with a BOM (byte order mark), an unprintable combination of bits announcing the nature of the file’s contents. Some utilities may require it.
Are strings mutable or immutable?
immutable
i_am = ‘I\‘m’
print(len(i_am))
What is the output?
3
i ‘ m
multiline = ‘Line #1
Line #2’
print(len(multiline))
What is the ouput?
Syntax Error
string cannot be on more than 1 line in ‘’ need to use ‘’’ ‘’’ or “”” “””
multiline = “"”Line #1
Line #2”””
print(len(multiline))
what is the output?
15
as there is a white space \n between the two lines that need to be counted
What are the two operations you can perform on strings?
concatenation +
replication *
What is overloading?
The ability to use the same operator against completely different kinds of data
What does ord do and what is its requirement?
ord() (ordinal) turns character in ASCII code point value
The function needs a one-character string as its argument - breaching this requirement causes a TypeError exception, and returns a number representing the argument’s code point.
What does char() do?
code point number –> corresponding character
ORD() NEEDS A STRING !!!!!!!!!!!
CHR() NEEDS A INT !!!!!!!!!!!!!!!
Strings are sequences therefore you can ….
iterate through them and index them
What is slicing?
taking a part of a string
e.g. x = kate
x[2:4]
–> te
alphabet = “abcdefghijklmnopqrstuvwxyz”
print(“f” in alphabet)
what is the output?
True
can also use not in
What three things can you not do with a string that you can with a list?
del (a specific element)
insert
append
alphabet = “bcdefghijklmnopqrstuvwxy”
alphabet = “a” + alphabet
What is the output?
“abcdefghijklmnopqrstuvwxy”
t = ‘The Knights Who Say “Ni!”’
min(t)
the space ‘ ‘
What will cause an error using the min()?
if the string or list is empty
Value Error
print(min(“aAbByYzZ”))
A
comes first in ASCII
print(max(“aAbByYzZ”))
z
print(“aAbByYzZaA”.index(“b”))
2
Note: the element searched for must occur in the sequence - its absence will cause a ValueError exception.
What does the index method do?
The index() method (it’s a method, not a function) searches the sequence from the beginning, in order to find the first element of the value specified in its argument.
The method returns the index of the first occurrence of the argument (which means that the lowest possible result is 0, while the highest is the length of argument decremented by 1).
What does the list function do?
The list() function takes its argument (a string) and creates a new list containing all the string’s characters, one per list element.
print(list(“abcabc”))
–>[‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]
What does the count method do?
The count() method counts all occurrences of the element inside the sequence. The absence of such elements doesn’t cause any problems.
for ch in “abc”:
print(chr(ord(ch) + 1), end=’’)
What is the output?
bcd
What does the capitalize() method do?
if the first character inside the string is a letter (note: the first character is an element with an index equal to 0, not just the first visible character), it will be converted to upper-case;
all remaining letters from the string will be converted to lower-case.
print(‘ Alpha’.capitalize())
’ alpha’
What does center() do?
The one-parameter variant of the center() method makes a copy of the original string, trying to center it inside a field of a specified width.
The centering is actually done by adding some spaces before and after the string
The two-parameter variant of center() makes use of the character from the second argument, instead of a space.