Source code for str.replace()

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.

I think it’s here:

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

See About GitHub Code Search - GitHub Docs . The file is too big: 446 KB vs limit of 350 KiB.

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.

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

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!