Published Nov 3, 2023
Contribute to Docs

The .padEnd() method in Kotlin is used to pad a string to a specific length by adding a specific character, sequence of characters, or space at the end of the original string. It returns a string of length equal to the specified length, appended as many times as necessary with the specified character to reach the specified length.


String.padEnd(length, padChar): String
  • String: The string to be padded.
  • length: The desired length of the resulting string after padding.
  • padChar: The character that will be added to the end of the string if the original string length is less than the desired length. Space is used by default.


This example shows how to use the .padEnd() method to append characters to different strings:

fun main() {
val padWithSpace = "Hello".padEnd(8)
val padWithCharacter = "Hello".padEnd(8, '!')
val noPad = "Hello".padEnd(3)

The code will return the following output:

'Hello '

All contributors

Looking to contribute?

Learn Kotlin on Codecademy