Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 298 characters in body
Source Link
Mark Tolonen
  • 180.8k
  • 26
  • 184
  • 279

When I run your original code, but use chcp 857 (the Turkish OEM code page) I can reproduce your issue, so I do not think you were running chcp 1254:

÷2しかく­3ÍæÌo▄

If you declare your source encoding as:

# -*- coding:cp1254 -*-

You must actually savesave your source code in that encoding. If you don't use Unicode strings, you must also use the same encoding at the console. Then it works correctly.

Example (source declared cp1254, but saved incorrectly as cp1252, and console chcp 1254):

öçisgüÖÇISGÜ

Example (source declared and saved correctly as cp1254, console chcp 1254):

öçışğüÖÇİŞĞÜ

It is important to note that with Unicode strings, you don't have to match the source encoding with the encoding of your console.

Example (declared and saved as UTF-8, with Unicode string):

#!python2
# -*- coding:utf8 -*-
print u"öçışğüÖÇİŞĞÜ"

Output (after chcp 1254use any code page that supports Turkish...1254, 857, 1026...):

öçışğüÖÇİŞĞÜ

If you declare your source encoding as:

# -*- coding:cp1254 -*-

You must actually save your source code in that encoding. Then it works correctly.

Example (source declared cp1254, but saved incorrectly as cp1252, and console chcp 1254):

öçisgüÖÇISGÜ

Example (source declared and saved correctly as cp1254, console chcp 1254):

öçışğüÖÇİŞĞÜ

It is important to note that with Unicode strings, you don't have to match the source encoding with the encoding of your console.

Example (declared and saved as UTF-8, with Unicode string):

#!python2
# -*- coding:utf8 -*-
print u"öçışğüÖÇİŞĞÜ"

Output (after chcp 1254):

öçışğüÖÇİŞĞÜ

When I run your original code, but use chcp 857 (the Turkish OEM code page) I can reproduce your issue, so I do not think you were running chcp 1254:

÷2しかく­3ÍæÌo▄

If you declare your source encoding as:

# -*- coding:cp1254 -*-

You must save your source code in that encoding. If you don't use Unicode strings, you must also use the same encoding at the console. Then it works correctly.

Example (source declared cp1254, but saved incorrectly as cp1252, and console chcp 1254):

öçisgüÖÇISGÜ

Example (source declared and saved correctly as cp1254, console chcp 1254):

öçışğüÖÇİŞĞÜ

It is important to note that with Unicode strings, you don't have to match the source encoding with the encoding of your console.

Example (declared and saved as UTF-8, with Unicode string):

#!python2
# -*- coding:utf8 -*-
print u"öçışğüÖÇİŞĞÜ"

Output (use any code page that supports Turkish...1254, 857, 1026...):

öçışğüÖÇİŞĞÜ
Source Link
Mark Tolonen
  • 180.8k
  • 26
  • 184
  • 279

If you declare your source encoding as:

# -*- coding:cp1254 -*-

You must actually save your source code in that encoding. Then it works correctly.

Example (source declared cp1254, but saved incorrectly as cp1252, and console chcp 1254):

öçisgüÖÇISGÜ

Example (source declared and saved correctly as cp1254, console chcp 1254):

öçışğüÖÇİŞĞÜ

It is important to note that with Unicode strings, you don't have to match the source encoding with the encoding of your console.

Example (declared and saved as UTF-8, with Unicode string):

#!python2
# -*- coding:utf8 -*-
print u"öçışğüÖÇİŞĞÜ"

Output (after chcp 1254):

öçışğüÖÇİŞĞÜ
lang-py

AltStyle によって変換されたページ (->オリジナル) /