0
0
Pythonprogramming~5 mins

String concatenation and repetition in Python

Choose your learning style9 modes available
Introduction

String concatenation lets you join words or sentences together. Repetition helps you repeat the same word or sentence multiple times easily.

When you want to join a first name and last name into a full name.
When you want to create a greeting message by joining 'Hello' and a person's name.
When you want to print a pattern like '***' by repeating a star character.
When you want to build a sentence by joining several smaller strings.
When you want to repeat a word multiple times for emphasis or decoration.
Syntax
Python
concatenation: string1 + string2
repetition: string * number

Use the plus sign (+) to join two or more strings together.

Use the asterisk (*) to repeat a string a certain number of times.

Examples
This joins 'John', a space, and 'Doe' to make 'John Doe'.
Python
full_name = "John" + " " + "Doe"
print(full_name)
This creates a greeting by joining 'Hi', a comma and space, and 'Alice!'.
Python
greeting = "Hi" + ", " + "Alice!"
print(greeting)
This repeats the star character 5 times to print '*****'.
Python
stars = "*" * 5
print(stars)
This repeats 'ha' three times to print 'hahaha'.
Python
laugh = "ha" * 3
print(laugh)
Sample Program

This program joins first and last names with a space, then repeats the word 'Wow! ' three times.

Python
first_name = "Emma"
last_name = "Brown"
full_name = first_name + " " + last_name
print("Full name:", full_name)

repeat_word = "Wow! " * 3
print("Repeated word:", repeat_word)
OutputSuccess
Important Notes

Remember to add spaces if you want spaces between words when concatenating.

Repetition only works with strings and a positive integer number.

Concatenation creates a new string; it does not change the original strings.

Summary

Use + to join strings together.

Use * to repeat a string multiple times.

Both help build new strings easily from smaller parts.