为什么Python的原始字符串不能以一个反斜杠结尾 python raw字符串为什么不能以反斜杠结尾
\u4e3a\u4ec0\u4e48Python\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u4e0d\u80fd\u4ee5\u4e00\u4e2a\u53cd\u659c\u6760\u7ed3\u5c3e\u8fd9\u4e2a\u8bf4\u6cd5\u5176\u5b9e\u4e0d\u597d\u3002
\u53cd\u659c\u6760\u4f1a\u7ed3\u5408\u540e\u4e00\u4e2a\u5b57\u7b26\u88ab\u89c6\u4f5c\u8f6c\u4e49\uff0c\u5982\u679c\u51fa\u73b0 \" \uff0c\u90a3\u4e48\u4f1a\u88ab\u8f6c\u4e49\u6389\uff0c\u7b49\u4e8e\u8bf4\u5b57\u7b26\u4e32\u7f3a\u4e86\u4e2a\u540e\u5f15\u53f7\u3002\u4f46\u662f\u5982\u679c\u4ee5 \\ \u7ed3\u5c3e\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5148\u88ab\u8f6c\u4e49\u6389\uff08\u673a\u5668\u4ece\u524d\u5411\u540e\u8bfb\uff0c\u770b\u5230\u53cd\u659c\u6760\u5c31\u5f00\u59cb\u8f6c\u4e49\uff09
\u8fd9\u662f\u4e00\u4e2aPython\u8bed\u6cd5\u89e3\u6790\u4e2d\u7684\u7455\u75b5\uff0c\u6ca1\u6709\u529e\u6cd5\u89e3\u51b3\uff0c\u6587\u6863\u91cc\u5df2\u7ecf\u660e\u786e\u8bf4\u660e\u4e0d\u53ef\u4ee5\u4e86
http://docs.python.org/reference/lexical_analysis.html
String quotes can be escaped with a backslash, but the backslash
remains in the string; for example, r"\"" is a valid string literal
consisting of two characters: a backslash and a double quote; r"\" is
not a valid string literal (even a raw string cannot end in an odd
number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).
Python\u8fd9\u4e48\u8bbe\u8ba1\u5927\u6982\u662f\u56e0\u4e3aPython\u89e3\u91ca\u5668\u5bf9.py\u6e90\u6587\u4ef6\u7684\u89e3\u6790\u8981\u6c42\u5427\u3002
\u53ef\u4ee5\u7528 r'abc'+'\\' \u7684\u5f62\u5f0f\u4ee3\u66ff\u3002
反斜杠会结合后一个字符被视作转义,如果出现
\"
,那么会被转义掉,等于说字符串缺了个后引号。但是如果以
\\
结尾,那么这两个先被转义掉(机器从前向后读,看到反斜杠就开始转义)
绛旓細鎺у埗娴佺▼锛**Python涓殑閫夋嫨缁撴瀯閲囩敤if-elif-else锛屼緷娆″垽鏂潯浠讹紝婊¤冻绗竴涓潯浠跺悗鎵ц鐩稿簲浠g爜銆傚瓧绗︿覆澶勭悊锛鍘熷瀛楃涓r鐢ㄤ簬閬垮厤杞箟瀛楃褰卞搷锛屽瓧绗︿覆鎿嶄綔鍖呮嫭涓嬫爣銆佷慨鏀广佸垹闄ゃ佹嫾鎺ワ紝浠ュ強鍐呯疆鍑芥暟濡俰sdigit()銆乺eplace()绛夈備緥濡傦紝s = "abcd"锛宲rint(s.swapcase())灏嗚浆鎹㈠ぇ灏忓啓銆傛暟鎹粨鏋勶細Python涓...
绛旓細Python鑷劧璇█澶勭悊鍏ラ棬锛3锛--澶勭悊鍘熷鏂囨湰鍦≒ython鑷劧璇█澶勭悊涓紝棣栬姝ラ鏄鐞嗗師濮嬫枃鏈紝鍖呮嫭鍒嗚瘝鍜屼粠HTML鏂囦欢涓彁鍙栧唴瀹广傚垎璇嶆槸灏嗙數瀛愪功涓鐨勫瓧绗︿覆鍒嗚В涓鸿瘝姹囧拰鏍囩偣绗﹀彿锛岄氳繃find()鍜宺find()鏂规硶鎵惧埌姝g‘鐨勫垏鐗囩储寮曘傚浜嶩TML鏂囨湰锛岀綉缁滀笂鐨勫ぇ閮ㄥ垎鏂囨湰閫氬父浠TML鏍煎紡瀛樺湪锛屽彲浠ユ姄鍙栫綉椤靛唴瀹瑰苟淇濆瓨涓烘湰...
绛旓細Python 3 鐨勬爣鍑嗗簱涓病澶氬皯鐢ㄦ潵瑙e喅鍔犲瘑鐨勶紝涓嶈繃鍗存湁鐢ㄤ簬澶勭悊鍝堝笇鐨勫簱銆傚鏋滈渶瑕佺敤鍒板畨鍏ㄥ搱甯岀畻娉曟垨鏄秷鎭憳瑕佺畻娉曪紝閭d箞浣犲彲浠ヤ娇鐢ㄦ爣鍑嗗簱涓殑 hashlib 妯″潡銆傝繖涓ā鍧楀寘鍚簡绗﹀悎 FIPS锛堢編鍥借仈閭︿俊鎭鐞嗘爣鍑嗭級鐨勫畨鍏ㄥ搱甯岀畻娉曪紝鍖呮嫭 SHA1锛孲HA224锛孲HA256锛孲HA384锛孲HA512 浠ュ強 RSA 鐨 MD5 绠楁硶銆侾ython ...
绛旓細鏈瘒鏂囩珷缁欏ぇ瀹跺垎浜殑鍐呭鏄叧浜巔ython瀛楃涓茶繛鎺ョ殑鍑犵鏂瑰紡鎬荤粨锛屾湁闇瑕佺殑鏈嬪弸鍙傝冧竴涓python涓湁寰堝瀛楃涓茶繛鎺ユ柟寮忥紝浠婂ぉ鍦ㄥ啓浠g爜锛岄『渚挎荤粨涓涓嬶紝浠庢渶鍘熷鐨勫瓧绗︿覆杩炴帴鏂瑰紡鍒板瓧绗︿覆鍒楄〃杩炴帴锛屽ぇ瀹舵劅鍙椾笅python涓湁寰堝瀛楃涓茶繛鎺ユ柟寮忥紝浠婂ぉ鍦ㄥ啓浠g爜锛岄『渚挎荤粨涓涓嬶細鏈鍘熷鐨勫瓧绗︿覆杩炴帴鏂瑰紡锛歴tr1 + str...
绛旓細琛ㄧず涓涓甯哥殑瀛楃涓诧紝閫氬父\n琚敤浣滄崲琛岋紝鑰+r鍚庯紝鍗宠〃绀篭n\n\n\n\n锛岃鐧戒簡灏辨槸鍘绘帀鍙嶆枩鏉犵殑杞箟鏈哄埗锛屽湪鏅氬瓧绗︿覆涓紝鍙嶆枩绾挎槸杞箟绗︼紝浠h〃涓浜涚壒娈婄殑鍐呭锛屽父鐢ㄤ簬鐗规畩鐨勫瓧绗︼紝濡傛崲琛岀銆佹鍒欒〃杈惧紡銆佹枃浠惰矾寰勩傛敞鎰忎笉鑳藉湪鍘熷瀛楃涓缁撳熬杈撳叆鍙嶆枩绾匡紝鍚﹀垯Python涓嶇煡閬撹繖鏄竴涓瓧绗﹁繕鏄崲琛岀(...
绛旓細s = input("璇疯緭鍏ヤ竴涓瀛楃涓")s1 = s.upper() # 鐢╯1淇濆瓨s杞崲鎴愬ぇ鍐欑殑鍊 s2 = s.lower() # 鐢╯2淇濆瓨s杞崲鎴愬皬鍐欑殑鍊 l = len(s) # 鐢╨淇濆瓨s鐨勯暱搴 print(s1) # 杈撳嚭s1鐨勫 print(s2) # 杈撳嚭s2鐨勫 print(l) # 杈撳嚭l鐨勫 ...
绛旓細瀛楃涓插拰瀛楄妭涓查兘鍙互鍔犲墠缂 'r' 鎴 'R'锛岀О涓 鍘熷瀛楃涓锛屽師濮嬪瓧绗︿覆鎶婂弽鏂滄潬褰撲綔鍘熶箟瀛楃锛屼笉鎵ц杞箟鎿嶄綔銆傚洜姝わ紝鍘熷瀛楃涓蹭笉杞箟 '\U' 鍜 '\u'銆備笌 Python 2.x 鐨勫師濮 unicode 瀛楅潰鍊兼搷浣滀笉鍚岋紝Python 3.x 鐜板凡涓嶆敮鎸 'ur' 鍙ユ硶銆3.3 鏂扮増鍔熻兘: 鏂板鍘熷瀛楄妭涓 'rb' 鍓嶇紑锛屾槸...
绛旓細鎵撳嵃涓庤緭鍑猴細print(), input()鍙橀噺: 鍙橀噺鍦ㄨ祴鍊肩殑鏃跺欑‘瀹氬彉閲忕殑绫诲瀷 妯″潡锛氶氳繃import 妯″潡鍚嶈繘琛屽姞杞芥ā鍧 Python鐨鏍囪瘑绗 鏍囪瘑绗︽槸鐢ㄦ埛缂栫▼鏃朵娇鐢ㄧ殑鍚嶅瓧锛岀敤浜庣粰鍙橀噺銆佸父閲忋佸嚱鏁般佽鍙ュ潡绛夊懡鍚嶏紝浠ュ缓绔嬭捣鍚嶇О涓庝娇鐢ㄤ箣闂寸殑鍏崇郴銆傛爣璇嗙閫氬父鐢卞瓧姣嶅拰鏁板瓧浠ュ強鍏跺畠瀛楃鏋勬垚銆傛爣璇嗙鐨勫懡鍚嶉伒寰互涓嬭瀹氾細寮澶...
绛旓細'hello\nworld')锛岃緭鍑虹粨鏋滐細鑰屼綘杩欓噷鐨剅'\x'浣滄嫾鎺ワ紝鍦ㄦ嫾鎺ュ畬鎴愪箣鍓嶅氨宸茬粡瀵'\x'闃叉杩囦簡锛屽啀鎷兼帴鏃惰浆涔夌\鍙堝彂鎸ヤ綔鐢ㄤ簡銆俓x44\xD1鏄痯ython2缂栫爜闂鏃犳硶姝e父鏄剧ず瀛楃锛堟瘮濡備腑鏂囷級锛鐢╬ython3鏄彲浠ユ甯告樉绀虹殑銆傝s杞负鏈夌壒娈婃剰涔鐨勫瓧绗︿覆锛屾病鏈変换浣曟剰涔夛紝浠ュ悗寮鍙戜篃鏍规湰鐢ㄤ笉鍒 ...
绛旓細姝e垯琛ㄨ揪寮忓尮閰嶅弽鏂滄潬"\"锛涓轰粈涔鏄"\\\"鎴栨槸 r"\\"鍛紵鍥犱负鍦ㄦ鍒欒〃杈惧紡涓璡涓虹壒娈婄鍙凤紝涓轰簡鍙栨秷瀹冨湪姝e垯琛ㄨ揪寮忎腑鐨勭壒娈婃剰涔夐渶瑕佸姞涓涓猏灏卞彉鎴愪簡\\锛屼絾鏄棶棰樺張鏉ヤ簡锛孿涔熸槸瀛楃涓蹭腑鐨勭壒娈婂瓧绗︼紝鎵浠ュ張瑕佸垎鍒涓や釜\鍙栨秷鍏剁壒娈婃剰涔夛紝鍗充负\\\銆Python涓湁涓涓鍘熷瀛楃涓鎿嶄綔绗︼紝鐢ㄤ簬閭d簺瀛楃涓蹭腑...