convert_str

opthandler.convert_str(s, strip=True, case_sensitive=False, empty_none=False, extended_bool=False, bool_01=False)[source]

Convert the input to its corresponding type.

Convert the input to NoneType, boolean, integer or float depending on its content. If a conversion in the aforementioned types is not possible, the input is returned as is.

Parameters:
  • s (str_like) – The input. Can be anything that can be converted to a string.

  • strip (bool, optional) – Whether to strip leading and trailing spaces before processing the input string.

  • case_sensitive (bool, optional) – Whether to be case sensitive. If True, only upper case strings are convertet to their corresponding types.

  • empty_none (bool, optional) – If True, convert the empty string '' to the NoneType None.

  • extended_bool (bool, optional) – If True, also convert 'Yes'/'No' and 'On'/'Off' to True/False.

  • bool_01 (bool, optional) – If True, also convert 0/1 to True/False.

Returns:

result (None or bool or int or float or str) – The converted string or the input as is.

See also

str2none()

Convert the string 'None' to the NoneType None

Examples

Conversion to NoneType None:

>>> convert_str('None')  # Returns None
>>> convert_str('none')  # Returns None
>>> convert_str('none', case_sensitive=True)
'none'
>>> convert_str('')
''
>>> convert_str('', empty_none=True)  # Returns None

Conversion to boolean True:

>>> convert_str('True')
True
>>> convert_str('true')
True
>>> convert_str('true', case_sensitive=True)
'true'
>>> convert_str('Yes')
'Yes'
>>> convert_str('Yes', extended_bool=True)
True
>>> convert_str('yes', extended_bool=True)
True
>>> convert_str('yes', extended_bool=True, case_sensitive=True)
'yes'
>>> convert_str('On')
'On'
>>> convert_str('On', extended_bool=True)
True
>>> convert_str('on', extended_bool=True)
True
>>> convert_str('on', extended_bool=True, case_sensitive=True)
'on'
>>> convert_str('1')
1
>>> convert_str('1', bool_01=True)
True

Conversion to boolean False:

>>> convert_str('False')
False
>>> convert_str('false')
False
>>> convert_str('false', case_sensitive=True)
'false'
>>> convert_str('No')
'No'
>>> convert_str('No', extended_bool=True)
False
>>> convert_str('no', extended_bool=True)
False
>>> convert_str('no', extended_bool=True, case_sensitive=True)
'no'
>>> convert_str('Off')
'Off'
>>> convert_str('Off', extended_bool=True)
False
>>> convert_str('off', extended_bool=True)
False
>>> convert_str('off', extended_bool=True, case_sensitive=True)
'off'
>>> convert_str('0')
0
>>> convert_str('0', bool_01=True)
False

Conversion to integer:

>>> convert_str('123')
123
>>> convert_str(' 123 ')  # Regardless if strip is True or False
123
>>> convert_str('a123')
'a123'

Conversion to float:

>>> convert_str('123.456')
123.456
>>> convert_str(' 123.456 ')  # Regardless if strip is True or False
123.456
>>> convert_str('a123.456')
'a123.456'

No conversion (input returned as is):

>>> # strip has no effect if no conversion takes place.
>>> convert_str(' a string ', strip=False)
' a string '
>>> convert_str(' a string ', strip=True)
' a string '
>>> # case_sensitive has no effect if no conversion takes place.
>>> convert_str('A sTrInG', case_sensitive=False)
'A sTrInG'
>>> convert_str('A sTrInG', case_sensitive=True)
'A sTrInG'