String Formatting in Python
Given a string:
txt = 'a1bc11de'
Remove all ones from the string to get the following result:
['a', 'b', 'c', 'd']
Given a string with trailing spaces:
txt = ' abcde '
Remove spaces from the ends:
'abcde'
Given a string with trailing spaces:
txt = ' abcde '
Remove spaces on the left:
'abcde '
Given a string with trailing spaces:
txt = ' abcde '
Remove spaces on the right:
' abcde'
Given a string with trailing spaces:
txt = ' abcde '
Remove spaces on the right:
' abcde'
Given a string and a number:
txt = 'abc {}'
num = 12
Substitute a number in place of the curly brackets in the line.
Let's say you have a string variable with a username followed by curly braces. Pass in the user's age using the variable age.
Given an empty string:
txt = ''
Given a variable:
num = 6
Write as many zeros into the line as the value contained in our variable:
'000000'
Given a string:
txt = 'abcde'
Write the code to get the following result:
'abcde111'
Given a string:
txt = '12345'
Write the code to get the following result:
'aa12345'