Why is it when I strip my string it takes out a character I need in python? -
i new programming , trying use python me data. part, when .rstrip('') want. there few times in when ask print stripped version, noticed took off 'm' did not intend take out. doing wrong?
an example of use this:
x = 'somethingunwanted_mo-wantedstuff' f = x.rstrip('somethingunwanted_') print f what want in case 'mo-wantedstuff'. every , again however, when hit print o-wantedstuff. doing wrong?
update: realized mistyped pointed out me. fixed that.
you seem confused rstrip does:
in [21]: x = 'somethingunwanted_mo-wantedstuff' f = x.rstrip('somethingunwanted_mo-wantedstuff') f out[21]: '' vs:
in [18]: x = 'somethingunwanted_mo-wantedstuff' f = x.split('_')[1] f out[18]: 'mo-wantedstuff' the docs:
return copy of string trailing characters removed. if chars omitted or none, whitespace characters removed. if given , not none, chars must string; characters in string stripped end of string method called on.
edit
again seem confused after edit, above passed in matching sequence removed right side in case nothing wil removed:
in [23]: x = 'somethingunwanted_mo-wantedstuff' f = x.rstrip('somethingunwanted_') f out[23]: 'somethingunwanted_mo-wantedstuff' you want strip if want strip passed in match left side:
in [24]: x = 'somethingunwanted_mo-wantedstuff' f = x.strip('somethingunwanted_') f out[24]: 'mo-wantedstuff' as shown @chapelo if you're looking remove string position can use replace:
in [25]: x = 'somethingunwanted_mo-wantedstuff' f = x.replace('somethingunwanted_','') f out[25]: 'mo-wantedstuff'
Comments
Post a Comment