Trong chương trình này, bạn sẽ học cách tìm giá trị ASCII của một ký tự và hiển thị nó.
Để hiểu ví dụ này, bạn nên có kiến thức về các chủ đề lập trình Python sau:
- Đầu vào, đầu ra và nhập khẩu của Python
- Lập trình Python Chức năng tích hợp
ASCII là viết tắt của mã tiêu chuẩn Mỹ để trao đổi thông tin.
Đó là một giá trị số được cung cấp cho các ký tự và ký hiệu khác nhau, cho các máy tính lưu trữ và thao tác. Ví dụ: giá trị ASCII của chữ cái
The ASCII value of 'p' is 1127 là 65.
Mã nguồn
# Program to find the ASCII value of the given character
c = 'p'
print["The ASCII value of '" + c + "' is", ord[c]]
Đầu ra
The ASCII value of 'p' is 112
Lưu ý: Để kiểm tra chương trình này & nbsp; cho các ký tự khác, hãy thay đổi ký tự được gán thành biến The ASCII value of 'p' is 112
8. To test this program for other characters, change the character assigned to the
The ASCII value of 'p' is 1128 variable.
Ở đây chúng tôi đã sử dụng hàm
The ASCII value of 'p' is 1129 để chuyển đổi ký tự thành số nguyên [giá trị ASCII]. Hàm này trả về điểm mã Unicode của ký tự đó.
Unicode cũng là một kỹ thuật mã hóa cung cấp một số duy nhất cho một ký tự. Mặc dù ASCII chỉ mã hóa 128 ký tự, Unicode hiện tại có hơn 100.000 ký tự từ hàng trăm tập lệnh.
Lần lượt của bạn: Sửa đổi mã ở trên để lấy các ký tự từ các giá trị ASCII tương ứng của chúng bằng hàm chr [] như hiển thị bên dưới. Modify the code above to get characters from their corresponding ASCII values using the chr[] function as shown below.
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'
Ở đây,
The ASCII value of 'p' is 1129 và
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'1 là các chức năng tích hợp. Truy cập ở đây để biết thêm về các chức năng tích hợp trong Python.
ASCII là viết tắt của mã tiêu chuẩn Mỹ để trao đổi thông tin. Đây là một tiêu chuẩn mã hóa ký tự sử dụng các số từ 0 đến 127 để biểu diễn các ký tự tiếng Anh. Ví dụ: mã ASCII cho ký tự A là 65 và 90 là cho Z. Tương tự, mã ASCII 97 dành cho A và 122 dành cho z. Mã ASCII cũng được sử dụng để đại diện cho các ký tự như Tab, Form Feed, Trở lại vận chuyển và một số ký hiệu.
Phương pháp
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'2 trong Python trả về một chuỗi chứa một biểu diễn có thể in của một đối tượng cho các ký tự không phải là alphabets hoặc vô hình như tab, trả về vận chuyển, thức ăn mẫu, v.v. trốn thoát.
Syntax:
ascii[object]
Parameters:
Đối tượng: Bất kỳ loại đối tượng.
Loại trở lại:
Trả về một chuỗi.
Phương thức
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'2 trả về một ký tự trả về vận chuyển có thể in trong một chuỗi, như được hiển thị bên dưới.
mystr='''this
is a new line.'''
print[ascii[mystr]]
Trong ví dụ trên,
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'7 trỏ vào một chuỗi với return vận chuyển, có một chuỗi trong dòng mới. Nó là một ký tự vô hình trong chuỗi. Phương thức
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'2 trả về một chuỗi có thể in chuyển đổi một lần quay lại vận chuyển thành char \ n có thể in được. Xin lưu ý rằng nó không chuyển đổi các ký tự tiếng Anh khác.
Biểu tượng in ví dụ sau đây Ø Sử dụng phương thức
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'2:
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
'A string in python.'
'A string in pyth\xd8n.'
Trong ví dụ trên, mã ASCII cho Ø là thập phân 216 và thập lục phân D8, được biểu thị bằng tiền tố \ x, \ xD8. Vì vậy, phương thức
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'2 chuyển đổi Ø thành \ xD8 trong một chuỗi.
Nó thoát khỏi các ký tự không phải ASCII trong chuỗi bằng cách sử dụng
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'3,
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'4 hoặc
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'5 thoát ra.
Sau đây cho thấy phương pháp
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'2 với danh sách.
Languages = ['pythØn','C++','Go']
print[ascii[Languages]]
['pyth\xd8n', 'C++', 'Go']
ascii [] vs print []
Ví dụ sau đây cho thấy sự khác biệt giữa hàm
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'2 và print [].
print[ascii['PythØn']]
print['Pyth\xd8n']
Làm cách nào để in giá trị ASCII thành z?
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Đưa ra một ký tự, chúng ta cần in giá trị ASCII của nó bằng C/C ++/Java/Python.a
Output : 97Ví dụ:D
Output : 68
Đầu vào: A & NBSP; đầu ra: 97
- Đầu vào: Doutput: 68 ord[] : It converts the given string of length one, returns an integer representing the Unicode code point of the character. For example, ord[‘a’] returns the integer 97.
Dưới đây là một số phương thức trong các ngôn ngữ lập trình khác nhau để in giá trị ASCII của một ký tự nhất định: & nbsp;
Mã Python bằng hàm Ord: Ord []: Nó chuyển đổi chuỗi đã cho có độ dài một, trả về một số nguyên đại diện cho điểm mã unicode của ký tự. Ví dụ: ord [‘a,] trả về số nguyên 97. & nbsp;
Python
Đầu ra
The ASCII value of 'p' is 1120
Độ phức tạp về thời gian: O [1] Không gian phụ trợ: O [1]
Auxiliary Space: O[1]
6ascii[object]
7ascii[object]
8We use format specifier here to give numeric value of character. Here %d is used to convert character to its ASCII value.ascii[object]
ascii[object]
9mystr='''this
is a new line.'''
print[ascii[mystr]]
0mystr='''this
is a new line.'''
print[ascii[mystr]]
1 mystr='''this
is a new line.'''
print[ascii[mystr]]
2 ascii[object]
6mystr='''this
is a new line.'''
print[ascii[mystr]]
2 mystr='''this
is a new line.'''
print[ascii[mystr]]
5__
ascii[object]
mystr='''this
is a new line.'''
print[ascii[mystr]]
mystr='''this
is a new line.'''
print[ascii[mystr]]
mystr='''this
is a new line.'''
print[ascii[mystr]]
ascii[object]
mystr='''this
is a new line.'''
print[ascii[mystr]]
mystr='''this
is a new line.'''
print[ascii[mystr]]
mystr='''this
is a new line.'''
print[ascii[mystr]]
9Mã C: Chúng tôi sử dụng định dạng xác định định dạng ở đây để cung cấp giá trị số của ký tự. Ở đây %D được sử dụng để chuyển đổi ký tự thành giá trị ASCII của nó. & NBSP;
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
2C
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
0 NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
1NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
4 NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
55____56NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
7'A string in python.'
'A string in pyth\xd8n.'
6Đầu ra
The ASCII value of 'p' is 1121
Độ phức tạp về thời gian: O [1] Không gian phụ trợ: O [1]O[1]
Auxiliary Space: O[1]
3NormalText = "A string in python." SpecialText = "A string in pythØn." print[ascii[NormalText]] print[ascii[SpecialText]]
9NormalText = "A string in python." SpecialText = "A string in pythØn." print[ascii[NormalText]] print[ascii[SpecialText]]
0mystr='''this is a new line.''' print[ascii[mystr]]
1'A string in python.' 'A string in pyth\xd8n.'
2Here int[] is used to convert a character to its ASCII value.'A string in python.' 'A string in pyth\xd8n.'
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3'A string in python.'
'A string in pyth\xd8n.'
4 'A string in python.'
'A string in pyth\xd8n.'
5
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
'A string in python.'
'A string in pyth\xd8n.'
'A string in python.'
'A string in pyth\xd8n.'
'A string in python.'
'A string in pyth\xd8n.'
7Mã C ++: Ở đây int [] được sử dụng để chuyển đổi một ký tự thành giá trị ASCII của nó. & NBSP;
Mã C: Chúng tôi sử dụng định dạng xác định định dạng ở đây để cung cấp giá trị số của ký tự. Ở đây %D được sử dụng để chuyển đổi ký tự thành giá trị ASCII của nó. & NBSP;
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
2C
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
0 NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
1NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
4 NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
55____56NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
7'A string in python.'
'A string in pyth\xd8n.'
6Đầu ra
The ASCII value of 'p' is 1122
Độ phức tạp về thời gian: O [1] Không gian phụ trợ: O [1]O[1]
Auxiliary Space: O[1]
3NormalText = "A string in python." SpecialText = "A string in pythØn." print[ascii[NormalText]] print[ascii[SpecialText]]
9NormalText = "A string in python." SpecialText = "A string in pythØn." print[ascii[NormalText]] print[ascii[SpecialText]]
0mystr='''this is a new line.''' print[ascii[mystr]]
1'A string in python.' 'A string in pyth\xd8n.'
2 Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, Java converts the character value to an ASCII value.'A string in python.' 'A string in pyth\xd8n.'
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3'A string in python.'
'A string in pyth\xd8n.'
4 'A string in python.'
'A string in pyth\xd8n.'
5
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
'A string in python.'
'A string in pyth\xd8n.'
'A string in python.'
'A string in pyth\xd8n.'
Mã C ++: Ở đây int [] được sử dụng để chuyển đổi một ký tự thành giá trị ASCII của nó. & NBSP;
CPP
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
2'A string in python.'
'A string in pyth\xd8n.'
8 'A string in python.'
'A string in pyth\xd8n.'
9 Languages = ['pythØn','C++','Go']
print[ascii[Languages]]
0NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
4 NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
5The ASCII value of 'p' is 1127
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
7NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3['pyth\xd8n', 'C++', 'Go']
0['pyth\xd8n', 'C++', 'Go']
1 ['pyth\xd8n', 'C++', 'Go']
2['pyth\xd8n', 'C++', 'Go']
3 ['pyth\xd8n', 'C++', 'Go']
4NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
0__NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3'A string in python.'
'A string in pyth\xd8n.'
6'A string in python.'
'A string in pyth\xd8n.'
6Đầu ra
The ASCII value of 'p' is 1123
Mã Java: Ở đây, để tìm giá trị ASCII của C, chúng tôi chỉ gán C cho một biến ASCII INT. Trong nội bộ, Java chuyển đổi giá trị ký tự thành giá trị ASCII. & NBSP;: O[1] // since no loop is used the algorithm
takes up constant time to perform the operations
Auxiliary Space: O[1] // since no extra array is used so the space taken by the algorithm is constant
- Java Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, C# converts the character value to an ASCII value.
print[ascii['PythØn']]
print['Pyth\xd8n']
1 print[ascii['PythØn']]
print['Pyth\xd8n']
2 print[ascii['PythØn']]
print['Pyth\xd8n']
3
print[ascii['PythØn']]
print['Pyth\xd8n']
print[ascii['PythØn']]
print['Pyth\xd8n']
print[ascii['PythØn']]
print['Pyth\xd8n']
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3print[ascii['PythØn']]
print['Pyth\xd8n']
1 print[ascii['PythØn']]
print['Pyth\xd8n']
6 print[ascii['PythØn']]
print['Pyth\xd8n']
7 print[ascii['PythØn']]
print['Pyth\xd8n']
8The ASCII value of 'p' is 11201
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
4 NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
55____1044The ASCII value of 'p' is 11201
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
0 The ASCII value of 'p' is 11208
The ASCII value of 'p' is 11201
The ASCII value of 'p' is 11210
['pyth\xd8n', 'C++', 'Go']
1 The ASCII value of 'p' is 11212
The ASCII value of 'p' is 11213
The ASCII value of 'p' is 11214
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
2'A string in python.'
'A string in pyth\xd8n.'
8 'A string in python.'
'A string in pyth\xd8n.'
9 Languages = ['pythØn','C++','Go']
print[ascii[Languages]]
0NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
4 NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
5The ASCII value of 'p' is 1127
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
7NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3['pyth\xd8n', 'C++', 'Go']
0['pyth\xd8n', 'C++', 'Go']
1 ['pyth\xd8n', 'C++', 'Go']
2['pyth\xd8n', 'C++', 'Go']
3 ['pyth\xd8n', 'C++', 'Go']
4NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
0__Mã Java: Ở đây, để tìm giá trị ASCII của C, chúng tôi chỉ gán C cho một biến ASCII INT. Trong nội bộ, Java chuyển đổi giá trị ký tự thành giá trị ASCII. & NBSP;
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3'A string in python.'
'A string in pyth\xd8n.'
6'A string in python.'
'A string in pyth\xd8n.'
6Đầu ra
The ASCII value of 'p' is 1123
Độ phức tạp về thời gian: O [1] Không gian phụ trợ: O [1]
Auxiliary Space: O[1]
Dưới đây là một phương pháp để in giá trị ASCII của các ký tự trong chuỗi bằng Python:
Python3
ascii[object]
9mystr='''this
is a new line.'''
print[ascii[mystr]]
0The ASCII value of 'p' is 11252
The ASCII value of 'p' is 11253
ascii[object]
7The ASCII value of 'p' is 11255
The ASCII value of 'p' is 11256
ascii[object]
7 The ASCII value of 'p' is 11258
The ASCII value of 'p' is 11259
The ASCII value of 'p' is 11260
ascii[object]
7 The ASCII value of 'p' is 11262
The ASCII value of 'p' is 11263
The ASCII value of 'p' is 11264
The ASCII value of 'p' is 11265
The ASCII value of 'p' is 11266
The ASCII value of 'p' is 11267
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3The ASCII value of 'p' is 11269
ascii[object]
7 mystr='''this
is a new line.'''
print[ascii[mystr]]
7The ASCII value of 'p' is 11272
NormalText = "A string in python."
SpecialText = "A string in pythØn."
print[ascii[NormalText]]
print[ascii[SpecialText]]
3ascii[object]
9The ASCII value of 'p' is 11275
The ASCII value of 'p' is 11276
The ASCII value of 'p' is 11277
Input:
The ASCII value of 'p' is 1125
Output:
The ASCII value of 'p' is 1126
Độ phức tạp về thời gian: O [n], trong đó n là độ dài của chuỗi đầu vào.AUXILIARY Không gian: O [1]O[N], where N is the
length of the input string.
Auxiliary Space: O[1]