Pad String to Specified Length in Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, sometimes we need to pad a string with a specific character to make it a certain length. For example, we may want to pad a string with spaces on both sides to make it a certain length. In this challenge, you will be tasked with writing a function that pads a string on both sides with the specified character, if it's shorter than the specified length.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} python/tuples -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} python/function_definition -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} python/default_arguments -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} python/importing_modules -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} python/using_packages -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} python/standard_libraries -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} python/math_random -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} python/build_in_functions -.-> lab-13703{{"`Pad String to Specified Length in Python`"}} end

Pad String

Write a function pad(s: str, length: int, char: str = ' ') -> str that pads a string on both sides with the specified character, if it's shorter than the specified length. The function should take in three parameters:

  • s: a string that needs to be padded
  • length: an integer that specifies the total length of the padded string
  • char: a character that is used to pad the string. The default value is a whitespace character.

The function should return the padded string.

from math import floor

def pad(s, length, char = ' '):
  return s.rjust(floor((len(s) + length)/2), char).ljust(length, char)
pad('cat', 8) ## '  cat   '
pad('42', 6, '0') ## '004200'
pad('foobar', 3) ## 'foobar'

Summary

In this challenge, you learned how to pad a string on both sides with the specified character, if it's shorter than the specified length. You used the str.ljust() and str.rjust() methods to pad both sides of the given string. You also learned how to use the whitespace character as the default padding character.

Other Python Tutorials you may like