0

I'm trying to catch strings with this condition:

if '_prod' in key:

But my string looks like "mystring_non-prod" and is catched by the if condition. And I have something weird if I print the variable:

key = 'mystring_non-prod'   
if '_prod' in key:
    name=key.strip('_prod')
    print(name)

    In [1]:
    mystring_non-

(I know I should use something else then str.strip())

It looks like the "_" (underscore) is not reconize by if. So I expect not to catch "mystring_non-prod" and catch then "mystring_prod" and correctly format the string.

key = 'mystring_prod'    
if '_prod' in key:
    name=key.strip('_prod')
    print(name)
    
 In [1]:
 mystring
Sanchez
  • 1
  • 2
  • 2
    Since python 3.9 ([PEP](https://www.python.org/dev/peps/pep-0616/)) there's [`str.removesuffix()`](https://docs.python.org/3/library/stdtypes.html#str.removesuffix) method. – Olvin Roght Jan 05 '22 at 10:28
  • I cannot reproduce your code. `'_prod' in key` evaluates to False for me. – joanis Jan 05 '22 at 20:07

1 Answers1

0

key = mystring_non-prod

In your example, the key is not containing "_prod" and it is not marked as a string

key = "mystring_non_prod"

Alin00
  • 23
  • 4