A string is a sequence of characters, like letters, numbers, or symbols, that are grouped together. It’s essentially a piece of text.
String manipulation refers to changing, combining, or extracting parts of a string to modify its content. It’s like playing with words or sentences to make them say what you want. For example, you can change the order of words, add new words, or remove some words from a sentence. In programming, string manipulation involves operations like finding the length of a string, converting it to uppercase or lowercase, replacing certain words or characters, splitting it into smaller parts, and joining different strings together. It’s all about working with text to make it do what you need it to do in your program.
If you love to learn watching tutorial side by side you can watch the tutorial below else you can scroll down to see the source code and explanation.
Source Code:
# Define a string variable
original_string = "Hello, World!"
# Print the original string
print("Original String:", original_string)
# Accessing individual characters in a string using indexing
first_char = original_string[0]
last_char = original_string[-1]
# Print the first and last characters
print("First Character:", first_char)
print("Last Character:", last_char)
# Concatenating strings
greeting = "Hello"
subject = "World"
concatenated_string = greeting + ", " + subject + "!"
print("Concatenated String:", concatenated_string)
# Finding the length of a string
string_length = len(original_string)
print("String Length:", string_length)
# Converting string to lowercase and uppercase
lowercase_string = original_string.lower()
uppercase_string = original_string.upper()
print("Lowercase String:", lowercase_string)
print("Uppercase String:", uppercase_string)
# Replacing substrings within a string
new_string = original_string.replace("World", "Universe")
print("New String after Replacement:", new_string)
# Splitting a string into a list of substrings
word_list = original_string.split(", ")
print("List of Words:", word_list)
# Joining a list of strings into a single string
joined_string = " ".join(word_list)
print("Joined String:", joined_string)
Let’s understand each code:
original_string = "Hello, World!": This line defines a string variableoriginal_stringwith the value “Hello, World!”.
2. print("Original String:", original_string): This line prints the original string.
3. first_char = original_string[0] and last_char = original_string[-1]: These lines demonstrate accessing individual characters in a string using indexing. It extracts the first and last characters of the original string.
4. print("First Character:", first_char) and print("Last Character:", last_char): These lines print the first and last characters extracted in the previous step.
5. concatenated_string = greeting + ", " + subject + "!": This line demonstrates concatenating strings by combining the greeting, subject, and punctuation.
6. print("Concatenated String:", concatenated_string): This line prints the concatenated string.
7. string_length = len(original_string): This line finds the length of the original string using the len() function.
8. print("String Length:", string_length): This line prints the length of the string.
9. lowercase_string = original_string.lower() and uppercase_string = original_string.upper(): These lines demonstrate converting a string to lowercase and uppercase, respectively.
10. print("Lowercase String:", lowercase_string) and print("Uppercase String:", uppercase_string): These lines print the lowercase and uppercase versions of the original string.
11. new_string = original_string.replace("World", "Universe"): This line replaces the substring “World” with “Universe” in the original string.
12. print("New String after Replacement:", new_string): This line prints the modified string after replacement.
13. word_list = original_string.split(", "): This line splits the original string into a list of substrings using the delimiter “, “.
14.print("List of Words:", word_list): This line prints the list of words obtained after splitting.
15. joined_string = " ".join(word_list): This line joins the list of words into a single string using a space as the separator.
16. print("Joined String:", joined_string): This line prints the joined string.





Leave a Reply