String Formatting in Python
striplstriprstripformatzfillljustrjustGiven a string:
pythontxt = 'a1bc11de'
Remove all ones from the string to get the following result:
python['a', 'b', 'c', 'd']
Given a string with trailing spaces:
pythontxt = ' abcde '
Remove spaces from the ends:
python'abcde'
Given a string with trailing spaces:
pythontxt = ' abcde '
Remove spaces on the left:
python'abcde '
Given a string with trailing spaces:
pythontxt = ' abcde '
Remove spaces on the right:
python' abcde'
Given a string with trailing spaces:
pythontxt = ' abcde '
Remove spaces on the right:
python' abcde'
Given a string and a number:
pythontxt = '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:
pythontxt = ''
Given a variable:
pythonnum = 6
Write as many zeros into the line as the value contained in our variable:
python'000000'
Given a string:
pythontxt = 'abcde'
Write the code to get the following result:
python'abcde111'
Given a string:
pythontxt = '12345'
Write the code to get the following result:
python'aa12345'