杰瑞科技汇

Python strip与split有何区别?

Of course! In Python, strip() and split() are two very common and useful string methods, but they do completely different things. Let's break them down one by one and then see how they are often used together.

Python strip与split有何区别?-图1
(图片来源网络,侵删)

strip() - Removing Unwanted Characters

The strip() method is used to remove leading and trailing characters (whitespace by default) from a string.

What it does:

  • Leading characters: Characters at the very beginning of the string.
  • Trailing characters: Characters at the very end of the string.
  • It does not remove characters from the middle of the string.

Syntax:

string.strip([characters])
  • characters (optional): A string specifying the set of characters to remove. If you don't provide it, it removes all whitespace (spaces, tabs, newlines).

Examples:

Example 1: Default Behavior (Removing Whitespace)

This is the most common use case. Imagine you get user input that might have accidental spaces at the beginning or end.

# A string with extra spaces at the beginning and end
text = "   Hello, World!   "
# Remove leading and trailing whitespace
cleaned_text = text.strip()
print(f"Original: '{text}'")
print(f"Cleaned:  '{cleaned_text}'")
# Output:
# Original: '   Hello, World!   '
# Cleaned:  'Hello, World!'

Example 2: Removing Specific Characters

Python strip与split有何区别?-图2
(图片来源网络,侵删)

You can tell strip() exactly which characters to remove from the ends.

# A string with unwanted punctuation
data = "##...data...##"
# Remove '#' and '.' from the ends
cleaned_data = data.strip('#.')
print(f"Original: '{data}'")
print(f"Cleaned:  '{cleaned_data}'")
# Output:
# Original: '##...data...##'
# Cleaned:  '...data...'

Variants: lstrip() and rstrip()

Python also has "left" and "right" versions of strip().

  • lstrip(): Removes only leading characters.
  • rstrip(): Removes only trailing characters.
text = "###Hello, World!###"
print(f"lstrip('#'): '{text.lstrip('#')}'")
# Output: lstrip('#'): 'Hello, World!###'
print(f"rstrip('#'): '{text.rstrip('#')}'")
# Output: rstrip('#'): '###Hello, World!'
print(f"strip('#'): '{text.strip('#')}'")
# Output: strip('#'): 'Hello, World!'

split() - Breaking a String into Parts

The split() method is used to break a string into a list of smaller substrings. It does this by splitting the string at each occurrence of a specified separator.

Python strip与split有何区别?-图3
(图片来源网络,侵删)

What it does:

  • Splits a string into a list of substrings.
  • The split happens at a specified separator.
  • If no separator is provided, it splits by any whitespace and discards empty strings.

Syntax:

string.split(separator, maxsplit)
  • separator (optional): The string to use as the delimiter for splitting. Default is any whitespace.
  • maxsplit (optional): The maximum number of splits to perform. If not provided, all possible splits are made.

Examples:

Example 1: Default Behavior (Splitting by Whitespace)

This is perfect for processing lines of text or comma-separated data where spaces might vary.

# A sentence with multiple spaces
sentence = "This  is a   test sentence"
# Split by any whitespace (spaces, tabs, etc.)
words = sentence.split()
print(f"Original: '{sentence}'")
print(f"Split list: {words}")
# Output:
# Original: 'This  is a   test sentence'
# Split list: ['This', 'is', 'a', 'test', 'sentence']

Example 2: Splitting by a Specific Separator

This is extremely common for parsing data formats like CSV (Comma-Separated Values).

# A comma-separated string
csv_data = "apple,banana,cherry,date"
# Split by the comma ','
fruits = csv_data.split(',')
print(f"Original: '{csv_data}'")
print(f"Split list: {fruits}")
# Output:
# Split list: ['apple', 'banana', 'cherry', 'date']

Example 3: Using maxsplit

The maxsplit argument is useful if you only want to split a string a certain number of times.

data = "one,two,three,four,five"
# Split only on the first comma
first_part, rest = data.split(',', 1)
print(f"First part: '{first_part}'")
print(f"Rest of the string: '{rest}'")
# Output:
# First part: 'one'
# Rest of the string: 'two,three,four,five'

How strip() and split() Work Together

This is where the real power comes in. A very common pattern is to clean up a string with strip() before splitting it with split().

Scenario: You read a line from a file that contains user data, but it has extra spaces and a newline character at the end.

# This could be a line read from a file
raw_line = "  John Doe, 30, New York  \n"
# Step 1: Clean the string with strip()
# This removes the leading/trailing spaces and the newline character '\n'
cleaned_line = raw_line.strip()
print(f"After strip(): '{cleaned_line}'")
# Output: After strip(): 'John Doe, 30, New York'
# Step 2: Split the cleaned string by the comma ','
# This gives us a list of the individual data items
data_parts = cleaned_line.split(',')
print(f"After split(): {data_parts}")
# Output: After split(): ['John Doe', ' 30', ' New York']
# Step 3: Clean up each part (notice there are still spaces!)
# We can use a list comprehension to strip each item in the list
final_data = [part.strip() for part in data_parts]
print(f"Final clean data: {final_data}")
# Output: Final clean data: ['John Doe', '30', 'New York']

Summary Table

Method Purpose What it Returns Example
strip() Removes leading/trailing characters. A new string with the characters removed. " hello ".strip() -> "hello"
split() Breaks a string into a list of substrings. A list of strings. "a,b,c".split(',') -> ['a', 'b', 'c']
lstrip() Removes only leading characters. A new string. "###hello".lstrip('#') -> "hello"
rstrip() Removes only trailing characters. A new string. "hello###".rstrip('#') -> "hello"

Key Takeaway

  • Use strip() when you want to clean up the beginning or end of a single string.
  • Use split() when you want to break apart a single string into a list of smaller strings.
  • They are often used in sequence: my_string.strip().split(',') to first clean a string and then parse it into parts.
分享:
扫描分享到社交APP
上一篇
下一篇