The ord
method is used to return the integer ordinal of a one-character string.
str.ord
str
: This is the one-character string that we want to get the integer ordinal of.
An integer value is returned which is the ordinal of a one-character string.
# create one-character stringsstr1 = "a"str2 = "b"str3 = "c"str4 = "z"# get integer ordinala = str1.ordb = str2.ordc = str3.ordd = str4.ord# print resultsputs aputs bputs cputs d
Line 2–5: We create one-character strings.
Line 8–11: We call the ord
method on the one-character strings and save the results on variables a
, b
, c
, and d
.
Line 14–17: We print the results, which are the integer ordinals of the string on the console.