Pad String to Specified Length in Python | Challenge

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-13171{{"`Pad String to Specified Length in Python | Challenge`"}} python/tuples -.-> lab-13171{{"`Pad String to Specified Length in Python | Challenge`"}} python/function_definition -.-> lab-13171{{"`Pad String to Specified Length in Python | Challenge`"}} python/default_arguments -.-> lab-13171{{"`Pad String to Specified Length in Python | Challenge`"}} python/importing_modules -.-> lab-13171{{"`Pad String to Specified Length in Python | Challenge`"}} python/using_packages -.-> lab-13171{{"`Pad String to Specified Length in Python | Challenge`"}} python/standard_libraries -.-> lab-13171{{"`Pad String to Specified Length in Python | Challenge`"}} python/math_random -.-> lab-13171{{"`Pad String to Specified Length in Python | Challenge`"}} python/build_in_functions -.-> lab-13171{{"`Pad String to Specified Length in Python | Challenge`"}} end

Pad String

Problem

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.

Example

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