Concatenation
String Concatenation
In Python, you can concatenate strings using the +
operator or by using the str.join()
method. Here are examples of both methods:
Method – Using the +
operator
str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result) # Output: Hello World
Method 2 – Using the str.join()
method
str1 = "Hello" str2 = "World" result = " ".join([str1, str2]) print(result) # Output: Hello World
Method 3 – Using f-strings (formatted string literals)
If you want to join multiples or mixed data types (like strings and integer) then f-strings are usually the easiest option. Otherwise you will need to use type casting with mystr = str(myint).
name = "Alice" age = 25 height = 1.65 message = f"My name is {name}, I am {age} years old, and my height is {height} meters." print(message) # Output: My name is Alice, I am 25 years old, and my height is 1.65 meters.
Slicing
String Slicing
In Python, string slicing allows you to extract a portion of a string by specifying a start and end index. The basic syntax for string slicing is string[start:end]
.
Example
text = "helloworld" # Extracting a substring from index 3 to 7 (excluding index 7) substring1 = text[3:7] print(substring1) # Output: lowo # Extracting a substring from index -5 to the end substring2 = text[-5:] print(substring2) # Output: world # Extracting a substring from index 0 to the end with a step of 2 substring3 = text[0::2] print(substring3) # Output: hlool # Reversing the string reversed_text = text[::-1] print(reversed_text) # Output: dlrowolleh
Negative Indexing
Negative indexing
Negative indexing allows you to slice a string starting from the end of the string.
text = "helloworld" # Extracting a substring starting from the second last character to the end substring1 = text[-2:] print(substring1) # Output: ld # Extracting a substring starting from the third last character to the fifth last character (excluding index -5) substring2 = text[-3:-5] print(substring2) # Output: # Extracting a substring starting from the beginning to the fourth last character substring3 = text[:-4] print(substring3) # Output: hellowo # Reversing the string using negative step value reversed_text = text[::-1] print(reversed_text) # Output: dlrowolleh
Methods
String Methods
Python provides several built-in string methods that you can use to perform various operations and manipulations on strings.
Challenges
Programming Challenges
Challenge 1
Print the first three characters of a given word.
Challenge 2
Create a new word by concatenating the last three characters of the first word and the first three characters of the second word.
Challenge 3
Print the last character of a given word.
Challenge 4
Create a new word by reversing the order of characters in a given word.
Challenge 5
Extract the middle three characters from a given word.
Challenge 6
Create a new word by concatenating the first and last characters of a given word.
Challenge 7
Create a new word by concatenating the middle character and the last two characters of a given word.
Challenge 8
Create a new word by repeating a given word three times with a space between each repetition.
Challenge 9
Create a new word by repeating each character of a given word twice.
Challenge 10
Create a new word by concatenating the even-indexed characters and odd-indexed characters of a given word separately.