DeKoss
DeKoss

Reputation: 447

StringTranslate() function in Python

In VFP there is a common function in most languages. It is: STRTRAN(var1,'abc') This function adjusts the var1 variable by taking out the sub-string "abc".

As an example:

STRTRAN("Hello yee valiant ones!", 'yee ') leads to "Hello valiant ones!"

Does python have anything like that? I only find some RegEx and confounding ways which is rather abstruse to me.

Many thanx for your help

DK

Upvotes: 2

Views: 404

Answers (2)

michaelmeyer
michaelmeyer

Reputation: 8215

There is a str.translate method but it only works for isolated characters. You probably want str.replace:

>>> "Hello yee valiant ones!".replace('yee ', '')
'Hello valiant ones!'

Upvotes: 2

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251186

You can use str.replace:

>>> strs = "Hello yee valiant ones!"
>>> strs.replace(' yee','')
'Hello valiant ones!'
#or
>>> strs.replace('yee ','')
'Hello valiant ones!'

Upvotes: 6

Related Questions