在Python中打印所有大写和小写字母

本文介绍了如何使用Python的内置函数ord()和chr(),结合ASCII值来打印所有的大写和小写字母。
摘要由CSDN通过智能技术生成

To do this task, we will use the concepts of ASCII value. ASCII stands for the American Standards Code for Information exchange. It provides us the numerical value for the representation of characters. The ASCII value of uppercase letters and lowercase alphabets start from 65 to 90 and 97-122 respectively. Before going to solve this problem, we will learn a little bit about how to convert the numerical value to characters and vice-versa.

为此,我们将使用ASCII value的概念。 ASCII代表美国信息交换标准代码。 它为我们提供了代表字符的数值。 大写字母和小写字母的ASCII值分别从65到90和97-122开始。 在解决此问题之前,我们将学习一些有关如何将数值转换为字符,反之亦然的知识。

Convert character to numerical value

将字符转换为数值

In Python, a function ord() is used to convert the characters into a numerical value. This is an inbuilt function. Let's see the program,

在Python中, 函数ord()用于将字符转换为数值。 这是一个内置功能。 让我们看一下程序,

# input a number
s=input('Enter the character: ')

# getting its ascii value
n=str(ord(s))

# printing the result
print('ASCII of character {} is {}.'.format(s,n))

Output

输出量

Enter the character: M
ASCII of character M is 77.

Convert numerical value to character

将数值转换为字符

In Python, a function chr() is used to convert a numerical value into character. This is an inbuilt function. Let's see the program,

在Python中, 函数chr(​​)用于将数值转换为字符。 这是一个内置功能。 让我们看一下程序,

# input a number i.e. ascii code
n=int(input('Enter the numerical value: '))

# getting its character value
s=chr(n)

# printing the result
print('The character value of {} is {}.'.format(s,str(n)))

Output

输出量

Enter the numerical value: 77
The character value of M is 77.

Now we have learned how to convert the numerical value into character and by using its concepts we will easily solve the above problem by using the Python language.

现在,我们已经学习了如何将数值转换为字符,并通过使用其概念,可以使用Python语言轻松解决上述问题。

Python程序可打印所有大写和小写字母 (Python program to print the all uppercase and lowercase alphabets)

# printing all uppercase alphabets
print("Uppercase Alphabets are:")
for i in range(65,91): 
        ''' to print alphabets with seperation of space.''' 
        print(chr(i),end=' ')  

# printing all lowercase alphabets
print("\nLowercase Alphabets are:")
for j in range(97,123): 
        print(chr(j),end=' ')

Output

输出量

Uppercase Alphabets are:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Lowercase Alphabets are:
a b c d e f g h i j k l m n o p q r s t u v w x y z


翻译自: https://www.includehelp.com/python/print-the-all-uppercase-and-lowercase-alphabets.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值