Convert matching double-quotes to single-quotes or vice versa in strings and streams. Inspired by the popular to-single-quotes npm package.
π Try it interactively in your browser! Test the library with our Interactive Playground - no installation required.
- Multiple input types: Convert quotes in strings and streams
- Command-line interface: Optional CLI tool for file and stream processing
- Proper escaping: Automatically handles quote escaping and unescaping
- Memory efficient: Process large texts with streaming without loading everything into memory
- Zero dependencies: Lightweight core library with no external dependencies
- Type safe: Full type hints for excellent IDE support
Why not just use .replace('"', "'")? Because simply replacing quotes breaks strings that contain nested quotes.
# The problem with simple replace
original = '"result = \'test\'"'
broken = original.replace('"', "'")
# Result: 'result = 'test'' -> Broken! Unmatched quotes
# The solution: quotes-convert handles escaping correctly
from quotes_convert import single_quotes
fixed = single_quotes(original)
# Result: 'result = \'test\'' -> Fixed! Properly escapedpip install quotes-convertfrom quotes_convert import single_quotes, double_quotes
result = single_quotes('x = "hello"; y = "world"')
print(result) # x = 'hello'; y = 'world'
result = double_quotes('x = "hello"; y = "world"')
print(result) # x = "hello"; y = "world"from quotes_convert import single_quotes, double_quotes
# Automatically escapes inner quotes
result = single_quotes('"it\'s working"')
print(result) # 'it\'s working'
result = double_quotes("'say \"hi\"'")
print(result) # "say \"hi\""Useful for normalizing JSON strings or Python dict definitions.
from quotes_convert import double_quotes
json_str = "{'key': 'value', 'nested': {'inner': 'data'}}"
result = double_quotes(json_str) # {"key": "value", "nested": {"inner": "data"}}from quotes_convert import single_quotes
script = 'echo "Hello $USER"; grep "pattern" file.txt'
result = single_quotes(script) # echo 'Hello $USER'; grep 'pattern' file.txtProcess large files or streams efficiently without loading the entire content into memory.
from quotes_convert import single_quotes_stream
def line_generator():
yield 'line 1: "hello"\n'
yield 'line 2: "world"\n'
# Process the stream chunk by chunk
for chunk in single_quotes_stream(line_generator()):
print(chunk, end='')
# Output:
# line 1: 'hello'
# line 2: 'world'The CLI tool allows you to convert quotes in files and streams directly from the command line.
pip install quotes-convert[cli]# Convert to single quotes
quotes-convert -s file.py
echo 'x = "hello"' | quotes-convert -s
# Convert to double quotes
quotes-convert -d file.py
# Edit file in place
quotes-convert -s -i file.py
# Check version
quotes-convert --versionFor complete documentation including batch processing, shell integration, and advanced usage, see the CLI Guide.
| Function | Description |
|---|---|
single_quotes(text: str) -> str |
Convert matching double-quotes to single-quotes. |
double_quotes(text: str) -> str |
Convert matching single-quotes to double-quotes. |
single_quotes_stream(stream: Iterable[str]) -> Generator[str, None, None] |
Convert matching double-quotes to single-quotes in a stream, yielding chunks. |
double_quotes_stream(stream: Iterable[str]) -> Generator[str, None, None] |
Convert matching single-quotes to double-quotes in a stream, yielding chunks. |
Inspired by Sindre Sorhus's to-single-quotes npm package.
See CHANGELOG.md for a detailed list of changes and version history.
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct, development setup, and the process for submitting pull requests.
If you find this library helpful:
- β Star the repository
- π Report issues
- π Submit pull requests
- π Sponsor on GitHub
MIT Β© Y. Siva Sai Krishna - see LICENSE file for details.
Author's GitHub β’ Author's LinkedIn β’ Report Issues β’ Package on PyPI β’ Package Documentation β’ Package Playground