Skip to main content

Replace specific substrings within a given string using index-based replacements

Tested against Windows / Python 3.11 / Anaconda

pip install indextextreplace

index_text_replace(string: str, replacements: List[Tuple[int, int, str]]) -> str:
    r"""
    Replace specific substrings within a given string using index-based replacements.

    Args:
        string (str): The input string in which replacements will be made.
        replacements (List[Tuple[int, int, str]]): A list of replacement tuples, where each tuple
            consists of three elements:
            - Start index (int): The starting index of the substring to be replaced (inclusive).
            - End index (int): The ending index of the substring to be replaced (inclusive).
            - Replacement text (str): The text to replace the specified substring with.

    Returns:
        str: A modified string with the specified replacements applied.



    This function replaces substrings within the input `string` according to the provided
    `replacements` list. Each replacement is defined by a start index (inclusive), an end index
    (inclusive), and the replacement text. The replacement is applied to the specified substring
    within the string. The modified string is then returned.

    If the `replacements` list contains overlapping intervals, the behavior of the function is
    defined by the order of the replacements within the list. The last replacement in the list
    for a given index takes precedence.

    Note:
    - Indexing is 0-based.
    - The function does not modify the original input string and returns a new modified string.
    - If the start or end indices of a replacement are out of bounds, they are adjusted to
      the nearest valid index within the string.

    Raises:
        ValueError: If the end index of a replacement is less than the start index."""
		
from indextextreplace import index_text_replace
t = '''Parenthese (altgriechisch παρένθεσις parénthesis, deutsch ‚Einschub')'''
r = [(1, 2, 'XXXX'), (13, 14, 'YYYYY')]
index_text_replace(t, r)
# "PXXXXenthese (aYYYYYgriechisch παρένθεσις parénthesis, deutsch ‚Einschub')"

Metadata

Release files for indextextreplace 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for indextextreplace 0.10
File Size Uploaded Provenance Actions
indextextreplace-0.10.tar.gz 3.6 kB Without provenance Details for indextextreplace-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for indextextreplace 0.10
File Interpreter ABI Platform Actions
indextextreplace-0.10-py3-none-any.whl Python 3 none any Details for indextextreplace-0.10-py3-none-any.whl

Total release size: 9.3 kB

Release files / indextextreplace-0.10.tar.gz

Download URL indextextreplace-0.10.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e889e6c457de2a9a4bf3ad6db109c79eeee0ae072f69e68b1c68f0c41f651efc
BLAKE2b-256 checksum
How to use checksums
8efac84ca15989a9ab0d86e23d3faebbce78f343aced1c1627408f41d49c5adc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / indextextreplace-0.10-py3-none-any.whl

Download URL indextextreplace-0.10-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31f681400c6af06a7c32b6cff987e7104014132994db2b8991ae7f2bf75005e6
BLAKE2b-256 checksum
How to use checksums
cc081ef3fea5ffae20055b1fa037df29edb23fce97fca2be0592fe188dd00b58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for indextextreplace 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page