MATLAB Examples

Pass MATLAB Backslash Control Character

This example shows how to pass the backslash control character (\) as a Python® str type.

Insert the new line control character \n by calling the MATLAB® sprintf function. Python replaces \n with a new line.

py.str(sprintf('The rain\nin Spain.'))
ans = 

  Python str with no properties.

    The rain
    in Spain.

Without the sprintf function, both MATLAB and Python interpret \ as a literal backslash.

py.str('The rain\nin Spain.')
ans = 

  Python str with no properties.

    The rain\nin Spain.

Pass this string to a Python string method split. Python treats the MATLAB character vector as a raw string and adds a \ character to preserve the original backslash.

split(py.str('The rain\nin Spain.'))
ans = 

  Python list with no properties.

    ['The', 'rain\\nin', 'Spain.']