Python replace command
<<<<
String Methods
The most basic way to manipulate strings is through the methods that are build into them. We can perform a limited number of tasks to strings through these methods. Open up the Python interactive interpreter. Let's create a string and play around with it a bit.
>>> test = 'This is just a simple string.'
Let's take a fast detour and use the len function. It can be used to find the length of a string. I'm not sure why it's a function rather than a method, but that's a whole nother issue:
>>> len ( test )
29
All right, now let's get back to those methods I was talking about. Let's take our string and replace a word using the replace method:
>>> test = test.replace ( 'simple', 'short' )
>>> testa
'This is just a short string.'
<<<<<
The replace command changes all instances of the first argument, not only the initial one.
String Methods
The most basic way to manipulate strings is through the methods that are build into them. We can perform a limited number of tasks to strings through these methods. Open up the Python interactive interpreter. Let's create a string and play around with it a bit.
>>> test = 'This is just a simple string.'
Let's take a fast detour and use the len function. It can be used to find the length of a string. I'm not sure why it's a function rather than a method, but that's a whole nother issue:
>>> len ( test )
29
All right, now let's get back to those methods I was talking about. Let's take our string and replace a word using the replace method:
>>> test = test.replace ( 'simple', 'short' )
>>> testa
'This is just a short string.'
<<<<<
The replace command changes all instances of the first argument, not only the initial one.

0 Comments:
Post a Comment
<< Home