Repeat Strings in Python

PythonPythonBeginner
Practice Now

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

Introduction

In Python, we can use the * operator to repeat a string a certain number of times. This can be useful in many situations, such as when we need to create a string with a specific number of characters or when we need to repeat a certain pattern.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ErrorandExceptionHandlingGroup -.-> python/custom_exceptions("`Custom Exceptions`") subgraph Lab Skills python/tuples -.-> lab-13166{{"`Repeat Strings in Python`"}} python/function_definition -.-> lab-13166{{"`Repeat Strings in Python`"}} python/custom_exceptions -.-> lab-13166{{"`Repeat Strings in Python`"}} end

Repeat String

Problem

Write a function called repeat_string that takes two parameters: a string s and an integer n. The function should return a new string that contains s repeated n times.

For example, if s is "hello" and n is 3, the function should return "hellohellohello". If s is "abc" and n is 5, the function should return "abcabcabcabcabc".

Example

assert repeat_string("hello", 3) == "hellohellohello"
assert repeat_string("abc", 5) == "abcabcabcabcabc"
assert repeat_string("123", 2) == "123123"

Summary

In this challenge, you learned how to use the * operator to repeat a string a certain number of times. You also wrote a function called repeat_string that takes a string and an integer as parameters and returns a new string that contains the original string repeated n times.

Other Python Tutorials you may like