开发者

Convert string / character to integer in python

I want to convert a single character of a string into an integer, add 2 to it, and then convert it back to a string. Hence, A becomes C, K开发者_开发技巧 becomes M, etc.


This is done through the chr and ord functions. Eg; chr(ord(ch)+2) does what you want. These are fully described here.


This sounds a lot like homework, so I'll give you a couple of pieces and let you fill in the rest.

To access a single character of string s, its s[x] where x is an integer index. Indices start at 0.

To get the integer value of a character it is ord(c) where c is the character. To cast an integer back to a character it is chr(x). Be careful of letters close to the end of the alphabet!

Edit: if you have trouble coming up with what to do for Y and Z, leave a comment and I'll give a hint.


Normally, Just ord and add 2 and chr back, (Y, Z will give you unexpected result ("[","\")

>>> chr(ord("A")+2)
'C'

If you want to change Y, Z to A, B, you could do like this.

>>> chr((ord("A")-0x41+2)%26+0x41)
'C'
>>> chr((ord("Y")-0x41+2)%26+0x41)
'A'
>>> chr((ord("Z")-0x41+2)%26+0x41)
'B'

Here is A to Z

>>> [chr((i-0x41+2)%26+0x41) for i in range(0x41,0x41+26)]
['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B']


http://docs.python.org/library/functions.html

ord(c)

Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string. For example, ord('a') returns the integer 97, ord(u'\u2020') returns 8224. This is the inverse of chr() for 8-bit strings and of unichr() for unicode objects. If a unicode argument is given and Python was built with UCS2 Unicode, then the character’s code point must be in the range [0..65535] inclusive; otherwise the string length is two, and a TypeError will be raised.


"ord" is only part of the solution. The puzzle you mentioned there rotates, so that "X"+3 rotates to "A". The most famous of these is rot-13, which rotates 13 characters. Applying rot-13 twice (rotating 26 characters) brings the text back to itself.

The easiest way to handle this is with a translation table.

import string

def rotate(letters, n):
    return letters[n:] + letters[:n]

from_letters = string.ascii_lowercase + string.ascii_uppercase
to_letters = rotate(string.ascii_lowercase, 2) + rotate(string.ascii_uppercase, 2)

translation_table = string.maketrans(from_letters, to_letters)

message = "g fmnc wms bgblr"
print message.translate(translation_table)

Not a single ord() or chr() in here. That's because I'm answering a different question than what was asked. ;)


Try ord(), should do the trick :)


For a whole string this would be:

>>> s = "Anne"
>>> ''.join([chr(ord(i)+2) for i in s]) 
'Cppg'

It's diffucult for 'Y', 'Z' ...

>>> s = "Zappa"
>>> ''.join([chr(ord(i)+2) for i in s]) 
'\\crrc'

Functions: chr, ord


For those who need to perform the operation on each character of the string, another way of handling this is by converting the str object to/from a bytes object which will take advantage of the fact that a bytes object is just a sequence of integers.

import numpy


old_text_str = "abcde"  # Original text
old_num_list = list(old_text_str.encode())  # Integer values of the original text
new_num_list = numpy.add(old_num_list, 2).tolist()  # Add +2 to the integer values
new_text_str = bytes(new_num_list).decode()  # Updated text

print(f"{old_text_str=}")
print(f"{old_num_list=}")
print(f"{new_num_list=}")
print(f"{new_text_str=}")

Output:

old_text_str='abcde'
old_num_list=[97, 98, 99, 100, 101]
new_num_list=[99, 100, 101, 102, 103]
new_text_str='cdefg'

Related topics:

  • How do I convert a list of ascii values to a string in python?
  • How can I convert a character to a integer in Python, and viceversa?
  • How to add an integer to each element in a list?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜