Other functions Flashcards
print()
print( “X”, “X”, “X”, sep=”x” )
print( “X”, end=”\n” ) —-change rows
print( “Y”, end=”” ) — dont chagne rows
print( “Z” )
format()
print( “The first three numbers are {:4}, {:4} and {:4}.”.format( “one”, “two”, “three” ) )
The first three numbers are one , two and three.
If you do not reserve sufficient space for a parameter with the precision, format() will take as much space as it needs. So you cannot use the precision to, for instance, break off a string prematurely.
print( “The first three numbers are {:>7}, {:^7} and {:<7}.”.format( “one”, “two”, “three” ) )
The first three numbers are one, two and three .
If you use precision, you can align the parameter to the left, center, or right. You do that by placing an alignment character between the colon and the precision. Alignment characters are “” for align right.
print( “{} divided by {} is {}”.format( 1, 2, 1/2 ) )
print( “ divided by {:d} is {:f}”.format( 1, 2, 1/2 ) )
print( “ divided by {:f} is {:f}”.format( 1, 2, 1/2 ) )
1 divided by 2 is 0.5
1 divided by 2 is 0.500000
1.000000 divided by 2.000000 is 0.500000
If you want a number to be interpreted as an integer, you place a “d” to the right side of the colon. If instead you want it to be interpreted as a float, you place an “f”. If you want to display an integer as a float, format() will do the necessary conversions for you. If you want to display a float as an integer, format() will cause a runtime error.
print( “ divided by {:5d} is {:5f}”.format( 1, 2, 1/2 ) )
print( “ divided by {:^5f} is {:>5f}”.format( 1, 2, 1/2 ) )
1 divided by 2 is 0.500000
1.000000 divided by 2.000000 is 0.500000
print( “ divided by {:.2f} is {:.2f}”.format( 1, 2, 1/2 ) )
1.00 divided by 2.00 is 0.50
you can indicate how many decimals you want a floating point number to be displayed with, by placing a period and an integer to the left of the f. format() will round the parameter to the requested number of decimals. Note that you can indicate zero decimals using .0, which will display floats as integers.
Write a function that gets as parameters two strings. The function returns the number of characters that the strings have in common. Each character counts only once, e.g., the strings “bee” and “peer” only have one character in common (the letter “e”). You can consider capitals different from lower case letters.
Note: the function should return the number of characters that the strings have in common, and not print it. To test the function, you can print the result of the function in your main program.
def remove_dup(text): chr = ''.join(set(text)) return chr
def isCommen(text1, text2): text1 = remove_dup(text1) text2 = remove_dup(text2) count = 0 for i in text1: for j in text2: if i ==j: count += 1 else: continue return count
isCommen(‘beeR’,’peer’)