Python function to operate strings

 submit   Problem Solving Skills, Human vs AI

Published Oct 01 '25

Fact  

Prompt: give me a python function that given an input string, split all substrings by comma, trim all substrings, return an output string with all non-empty trimmed substrings each wrapped using single quote concatenated using comma.

ChatGPT on 20251001

Got it 👍. Here’s a clean Python function for what you want:

```python def wrap_and_join(input_str: str) -> str: # Split by comma parts = input_str.split(",") # Trim, filter out empty, wrap with single quotes wrapped = [f"'{p.strip()}'" for p in parts if p.strip()] # Join with comma return ",".join(wrapped)

Example usage:

s = " apple, banana , , orange ,, grape " print(wrap_and_join(s))

Output: 'apple','banana','orange','grape'

```

This will:

Split the string by commas.

Strip whitespace from each piece.

Discard any empty pieces.

Wrap the remaining substrings in 'single quotes'.

Return them joined with commas.

Do you also want me to make it handle duplicate values (remove repeats), or keep all entries?


 

Terms of Use: You are in agreement with our Terms of Services and Privacy Policy. If you have any question or concern to any information published on SaveNowClub, please feel free to write to us at savenowclub@gmail.com. For product news and announcements, please check our blog.