String manipulation is a fundamental aspect of programming that involves modifying and transforming strings to meet specific needs. In Python, string manipulation is a powerful and straightforward process that can be achieved using a variety of built-in string methods and functions. In this article, we will explore some of the common string manipulation techniques in Python, along with practical examples to help you understand and apply them.

  1. Concatenation
    Concatenation refers to the process of joining two or more strings together to form a new string. In Python, we use the '+' operator to concatenate strings. Here's an example:
first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name print(full_name)

Output: John Doe

In the example above, we create two string variables named 'first_name' and 'last_name', then we use the '+' operator to concatenate them and store the result in a new variable called 'full_name'.

  1. Splitting
    Splitting refers to the process of breaking a string into smaller parts based on a specific delimiter. In Python, we use the 'split()' method to split a string. The 'split()' method takes an optional delimiter as an argument, and returns a list of the resulting substrings. Here's an example:
sentence = "The quick brown fox jumps over the lazy dog" words = sentence.split() print(words)

Output: ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

In the example above, we create a string variable named 'sentence', then we use the 'split()' method to split the sentence into individual words, which are stored as a list in the 'words' variable.

  1. Stripping
    Stripping refers to the process of removing leading and trailing characters from a string. In Python, we use the 'strip()' method to strip a string. The 'strip()' method takes an optional argument that specifies the characters to be removed. Here's an example:
text = " Hello, World! " clean_text = text.strip() print(clean_text)

Output: Hello, World!

In the example above, we create a string variable named 'text', which contains leading and trailing spaces. We then use the 'strip()' method to remove the spaces and store the result in a new variable called 'clean_text'.

  1. Replacing
    Replacing refers to the process of replacing one or more characters in a string with a new character or substring. In Python, we use the 'replace()' method to replace a string. The 'replace()' method takes two arguments: the old substring to be replaced, and the new substring to replace it with. Here's an example:
text = "Hello, World!" new_text = text.replace("World", "Python") print(new_text)

Output: Hello, Python!

In the example above, we create a string variable named 'text', which contains the word 'World'. We then use the 'replace()' method to replace the word 'World' with the word 'Python', and store the result in a new variable called 'new_text'.

  1. Formatting
    Formatting refers to the process of inserting variables or expressions into a string. In Python, we use the 'format()' method to format a string. The 'format()' method takes one or more arguments, which are used to replace placeholders in the string. Here's an example:
name = "Alice" age = 25 text = "My name is {} and I am {} years old".format(name, age) print(text)

Output: My name is Alice and I am 25 years old

In the example above, we create two variables named 'name' and 'age', which we use to replace the placeholders '{}' in the string.

In conclusion, string manipulation is a powerful tool that is essential for any Python programmer. By mastering these basic string manipulation techniques, you'll be able to write more efficient and flexible Python code that can handle a wide range of tasks.

文字列操作[JA]