First, you should almost never be using string.replace. As the docs say:
The following list of functions are also defined as methods of string and Unicode objects; see section String Methods for more information on those. You should consider these functions as deprecated…
Second, this is wrong for two separate reasons:
x = replace()
First, there is no builtin named replace, and there's no global in your module named replace either. If you did from string import * or from string import replace, then you wouldn't get this error—but if you were doing that, you could just do from string import replace as x, exactly as you're already doing for time.sleep.
Second, those parentheses mean that you're calling the function, and assigning its return value to x, not using the function itself as a value.
So, I think what you want is this:
x = str.replace
That's accessing the replace method on str objects, and storing it in x. An "unbound method" like this can be called by passing an instance of str (that is, any normal string) as the first argument. So:
x(my_string, ' ', '_')
If you want to add the name x as a method on the str class itself, what you want is called "monkeypatching", and normally, it's very simple:
str.x = str.replace
Unfortunately, it doesn't work with most of the built-in types, at least in CPython; you'll get an error like this:
TypeError: can't set attributes of built-in/extension type 'str'
You could of course create your own subclass of str, and use that all over the place instead of str… but that won't help with string literals, strings you get back from other functions, etc., unless you wrap them explicitly. And I'm not sure it's worth the effort. But if you want to:
class s(str):
x = str.replace
Now you can do this:
z = s(function_returning_a_string())
z = z.x(' ', '_')
But notice that at the end, z is back to being a str rather than an s, so if you want to keep using x, you have to do this:
z = s(z.x(' ', '_'))
… and at a certain point, even if you're saving a few keystrokes, you're not saving nearly enough for the cost in readabiity and idiomaticitalnessity.
string.replaceinstead of the methodreplaceonstrobjects? Unless you're writing Python 1.5…s = "hello world"; s = s.replace('hello', 'goodbye')