Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, replace long dash with short dash?

I want to replace a long dash () with a short dash (-). My code:

if " – " in string:
      string = string.replace(" – ", " - ")

results in the following error:

SyntaxError: Non-ASCII character '\xe2' in file ./script.py on line 76, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

How can I fix this?

like image 516
LewlSauce Avatar asked Oct 03 '13 01:10

LewlSauce


People also ask

How to get a dash instead of hyphen?

less simple: you need to use an alt code to get an em dash. If you have a numeric keyboard, hold down the Alt key and type 0151 for an em dash or 0150 for an en dash.

How do you make a long dash in Python?

Turn num lock on, and then hold down the left Alt key and type 0151 on the numeric keypad. Don't use an en dash (the shorter dash) or a hyphen in place of an em dash.

How do you replace a space in a dash in Python?

To replace a space with a dash in Python, the easiest way is to use the Python built-in string replace() function.

How to do a dash?

Android or iOS: Long-hold hyphen Long-hold the hyphen for access to the en dash and em dash with Gboard on Android or the default keyboard on iOS. The long-hold on the hyphen gives you access to four characters: A hyphen, an en dash, an em dash, and a bullet (Figure B).


1 Answers

Long dash is not an ASCII character. Declare encoding of your script, like this (somewhere on top):

#-*- coding: utf-8 -*-

There are also other encodings beside utf-8 but it is always safe to use utf-8 if not working with ASCII characters which covers virtually all (unicode) characters.

See PEP 0263 for more info.

like image 136
Santosh Kumar Avatar answered Sep 20 '22 11:09

Santosh Kumar