Skip to main content

force_str

Similar to smart_str(), except that lazy instances are resolved to strings, rather than kept as lazy objects.

If strings_only is True, don't convert (some) non-string-like objects.

def force_str(
s: any,
encoding: string = "utf-8",
strings_only: boolean = false,
errors: string = "strict"
) - > string

Similar to smart_str(), except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don't convert (some) non-string-like objects.

Parameters

NameTypeDescription
sanyThe input object to be converted into a string.
encodingstring = "utf-8"The character encoding to use when decoding byte strings.
strings_onlyboolean = falseIf True, prevents the conversion of non-string-like objects such as integers, floats, or None.
errorsstring = "strict"The error handling scheme to use for decoding errors (e.g., 'strict', 'ignore', 'replace').

Returns

TypeDescription
stringThe resulting string after conversion and decoding, or the original object if strings_only is enabled and the type is protected.