Having both single and double quotation in a python string

后端 未结 6 596
悲&欢浪女
悲&欢浪女 2020-11-29 09:19

Hi I\'m trying to have a string that contains both single and double quotation in python -- (\'\"). The reason I need this expression is to use as an input to some external

相关标签:
6条回答
  • 2020-11-29 09:37

    You can either (1) enclose the string in double quotes and escape the double quotes with a \ or (2) enclose the string in single quotes and escape the single quotes with a \. For example:

    >>> print('She is 5\' 6" tall.')
    She is 5' 6" tall.
    >>> print("He is 5' 11\" tall.")
    He is 5' 11" tall.
    
    0 讨论(0)
  • 2020-11-29 09:40

    Use triple-quoted strings:

    """ This 'string' contains "both" types of quote """
    ''' So ' does " this '''
    
    0 讨论(0)
  • 2020-11-29 09:42

    To add both the single and double quote on python use screened (escaped) quotes. Try this for example:

    print(" just display ' and \" ")
    

    The \" tells python this is not the end of the quoted string.

    0 讨论(0)
  • 2020-11-29 09:43

    The actual problem is , the print() statement doesn't print the \ but when you refer to the value of the string in the interpreter, it displays a "\" whenever an apostrophe is used . For instance, refer the following code:

        >>> s = "She said, \"Give me Susan's hat\""
        >>> print(s)
        She said, "Give me Susan's hat"
        >>> s
        'She said, "Give me Susan\'s hat"'
    

    This is irrespective of whether you use single, double or triple quotes to enclose the string.

        >>> s = """She said, "Give me Susan's hat" """
        >>> s
        'She said, "Give me Susan\'s hat" '
    

    Another way to include this :

        >>> s = '''She said, "Give me Susan's hat" '''
        >>> s
        'She said, "Give me Susan\'s hat" '
        >>> s =  '''She said, "Give me Susan\'s hat" '''
        >>> s
        'She said, "Give me Susan\'s hat" '
    

    So basically, python doesn't remove the \ when you refer to the value of s but removes it when you try to print. Despite this fact, when you refer to the length of s, it doesn't count the "\". For eg.,

        >>> s = '''"''"'''
        >>> s
        '"\'\'"'
        >>> print(s)
        "''"
        >>> len(s)
        4
    
    0 讨论(0)
  • 2020-11-29 09:57

    Although its more verbose, an alternative way would be to do the following:

    str1 = 'the part that has double "s" in it'

    str1 = str1 + " the part that has single 's' in it"

    0 讨论(0)
  • 2020-11-29 09:58

    Use triple quotes.

    """Trip'le qu"oted"""
    

    or

    '''Ag'ain qu"oted'''
    

    Keep in mind that just because Python reprs a string with backslashes, doesn't mean it's actually added any slashes to the string, it may just be showing special characters escaped.

    Using an example from the Python tutorial:

    >>> len('"Isn\'t," she said.')
    18
    >>> len('''"Isn't," she said.''')
    18
    

    Even though the second string appears one character shorter because it doesn't have a backslash in it, it's actually the same length -- the backslash is just to escape the single quote in the single quoted string.

    Another example:

    >>> for c in '''"Isn't," she said.''':
    ...     sys.stdout.write(c)
    ... 
    "Isn't," she said.
    >>> 
    

    If you don't let Python format the string, you can see the string hasn't been changed, it was just Python trying to display it unambiguously.

    See the tutorial section on strings.

    0 讨论(0)
提交回复
热议问题