# Source code for str.replace()

**URL:** <https://discuss.python.org/t/source-code-for-str-replace/108294>\
**Category:** Python Help\
**Created:** [July 27, 2026, 12:10pm UTC](https://discuss.python.org/t/source-code-for-str-replace/108294 "2026-07-27T12:10:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [July 27, 2026, 12:10pm UTC](https://discuss.python.org/t/source-code-for-str-replace/108294/1 "2026-07-27T12:10:07Z")

</div>

Can anyone tell me where I might find the underlying C source code for Python’s builtin str.replace() method?

Not that I know much about C but wanted to have a go at creating a .swap() method for searching an incoming string and swapping over two sub-strings. A bit like replace but swapping two strings instead.

---

<div class="post-metadata">

**Author:** ![mfile\_bay](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mfile\_bay](https://discuss.python.org/u/mfile_bay)\
**Post date:** [July 27, 2026, 1:07pm UTC](https://discuss.python.org/t/source-code-for-str-replace/108294/3 "2026-07-27T13:07:03Z")

</div>

I think it’s here:

> <https://github.com/python/cpython/blob/58d9c78d6aa45cf64237b571adbd0b3289a34559/Objects/unicodeobject.c#L12964>

By the way I usually do this by `help(str.replace)` then search a part of the docstring in the codebase. However this result happens to be out of GitHub code search for unknown reasons

---

<div class="post-metadata">

**Author:** ![chris-eibl](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/chris-eibl/32/25830_2.png) [@chris-eibl](https://discuss.python.org/u/chris-eibl)\
**Post date:** [July 27, 2026, 4:00pm UTC](https://discuss.python.org/t/source-code-for-str-replace/108294/4 "2026-07-27T16:00:34Z")

</div>

> [@mfile\_bay](#):
>
> for unknown reasons

See [About GitHub Code Search - GitHub Docs](https://docs.github.com/en/search-github/github-code-search/about-github-code-search#limitations) . The file is too big: 446 KB vs limit of 350 KiB.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [July 27, 2026, 10:51pm UTC](https://discuss.python.org/t/source-code-for-str-replace/108294/5 "2026-07-27T22:51:01Z")

</div>

In my local clone, main branch, with Python built, I ran IDLE (python -m idlelib), the help command, selected most of the first line of of the docstring, hit Find-in-files, reduced the search patch to /\*.c, checked recurse into subdirs, and go, and in a few seconds had unicode.c, bytes.c, and bytearray.c.

---

<div class="post-metadata">

**Author:** ![mfile\_bay](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@mfile\_bay](https://discuss.python.org/u/mfile_bay)\
**Post date:** [July 28, 2026, 12:54am UTC](https://discuss.python.org/t/source-code-for-str-replace/108294/6 "2026-07-28T00:54:52Z")

</div>

Yeah, local search is faster and better especially if your Internet connection is slow 🙂

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [July 28, 2026, 1:07am UTC](https://discuss.python.org/t/source-code-for-str-replace/108294/7 "2026-07-28T01:07:53Z")

</div>

Yep, and you can plan for this by taking a git clone of the repository (a shallow one if you don’t want all the history), and then you can use either “search across files” as Terry did, or the “git grep” command if you prefer the terminal. It’s all there, just a matter of finding it!
