2013-08-16 5 views
1

У меня есть переменнаяКак предотвратить закрытие "в переменной

sentence = "In 1794, shortly after his arrival in Manchester, Dalton was elected a member of the Manchester Literary and Philosophical Society, the "Lit & Phil", and a few weeks later he communicated his first paper on "Extraordinary facts relating to the vision of colours", in which he postulated that shortage in colour perception was caused by discoloration of the liquid medium of the eyeball. In fact, a shortage of colour perception in some people had not even been formally described or officially noticed until Dalton wrote about his own. Since both he and his brother were colour blind, he recognized that this condition must be hereditary." 

Теперь это может иметь как„“и„“из-за которой он закроет значение переменной. Я хочу, чтобы это предотвратить. есть ли другой способ хранения строки

+0

_ "Теперь это может иметь как'" "'и' '' '" _ Почему? Вы генерируете код python? Если это так, вам нужно избегать строк, когда вы их печатаете. – Eric

ответ

10

побег внедренные кавычки с \:

"Some text with \"embedded\" quotes" 

Если текст содержит только двойные кавычки, вы можете выделить цитаты и не должны бежать двойные кавычки:

'Some text with "embedded" quotes' 

Последнее, но не менее, вы можете тройные внешние цитаты и сохранить себе того, чтобы избежать новой строки тоже:

"""Some text with "embedded" quotes""" 
"""Some text with "embedded" quotes 
and a newline too""" 

Для примера, одиночные кавычки бы уже сделать трюк:

sentence = 'In 1794, shortly after his arrival in Manchester, Dalton was elected a member of the Manchester Literary and Philosophical Society, the "Lit & Phil", and a few weeks later he communicated his first paper on "Extraordinary facts relating to the vision of colours", in which he postulated that shortage in colour perception was caused by discoloration of the liquid medium of the eyeball. In fact, a shortage of colour perception in some people had not even been formally described or officially noticed until Dalton wrote about his own. Since both he and his brother were colour blind, he recognized that this condition must be hereditary.' 
1

Вы можете использовать тройные кавычки, как:

sentence = """ long sentence with all 'kind" of symbols """ 
4
a="""triple quoted strings can contain quote like this " without ending the string""" 
+3

, но, по-видимому, синтаксис SO не знает об этом: P –

+0

@SamueleMattiuzzo это, просто используйте: ''. –

+1

@AshwiniChaudhary: Я думаю, что это [ошибка] (http://meta.stackexchange.com/q/193495/147400). SO должно работать так, что из имени тега – Eric

Смежные вопросы