在Python中,isalpha()是用于字符串处理的内置方法。如果字符串中的所有字符都是字母,则isalpha()方法返回“True”,否则,返回“False”。此函数用于检查参数是否包含任何字母字符,例如:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

用法:

string.isalpha()

参数:

isalpha() does not take any parameters

返回:

1.True- If all characters in the string are alphabet.

2.False- If the string contains 1 or more non-alphabets.

例子:

Input:string = 'Ayush'

Output:True

Input:string = 'Ayush Saxena'

Output:False

Input:string = 'Ayush0212'

Output:False

# Python code for implementation of isalpha()

# checking for alphabets

string = 'Ayush'

print(string.isalpha())

string = 'Ayush0212'

print(string.isalpha())

# checking if space is an alphabet

string = 'Ayush Saxena'

print( string.isalpha())

输出:

True

False

False

错误和异常

它不包含任何参数,因此如果传递参数,则会发生错误

大写和小写字母都返回“True”

空格不被视为字母,因此它返回“False”

应用:给定python中的字符串,计算字符串中的字母数并打印字母。

例子:

Input:string = 'Ayush Saxena'

Output:11

AyushSaxena

Input:string = 'Ayush0212'

Output:5

Ayush

算法

1.将新的字符串和变量计数器初始化为0。

2.逐字符遍历给定的字符串字符直至其长度,检查字符是否为字母。

3.如果是字母,则将计数器增加1并将其添加到新字符串中,否则遍历下一个字符。

4.打印计数器的值和新字符串。

# Python program to illustrate

# counting number of alphabets

# using isalpha()

# Given string

string='Ayush Saxena'

count=0

# Initialising new strings

newstring1 =""

newstring2 =""

# Iterating the string and checking for alphabets

# Incrementing the counter if an alphabet is found

# Finally printing the count

for a in string:

if (a.isalpha()) == True:

count+=1

newstring1+=a

print(count)

print(newstring1)

#Given string

string='Ayush0212'

count=0

for a in string:

if (a.isalpha()) == True:

count+=1

newstring2+=a

print(count)

print(newstring2)

输出:

11

AyushSaxena

5

Ayush

python中isalpha的用法_Python string isalpha()用法及代码示例相关推荐

  1. python isnumeric函数用法_Python string isnumeric()用法及代码示例

    在Python中,isnumeric()是用于字符串处理的内置方法.如果字符串中的所有字符均为数字字符,则issnumeric()方法返回"True",否则,返回"Fal ...

  2. python中count的作用_python count函数用法详解

    在python中可以使用"count()"函数统计字符串里某个字符出现的次数,该函数用于统计次数,其语法是"count(sub, start= 0,end=len(str ...

  3. python中图例legend标签内容_matplotlib设置legend图例代码示例

    matplotlib设置legend图例代码示例 本文主要是关于matplotlib的一些基本用法. Demo import matplotlib.pyplot as plt import numpy ...

  4. python画折线图虚线_python绘制简单折线图代码示例

    1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...

  5. python中的get函数_python之函数用法get()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法get() #http://www.runoob.com/python/att-dic ...

  6. python中sinh是什么_Python PyTorch sinh()用法及代码示例

    PyTorch是由Facebook开发的开源机器学习库.它用于深度神经网络和自然语言处理. 功能torch.sinh()为PyTorch中的双曲正弦函数提供支持.它期望以弧度形式输入.输入类型为张量, ...

  7. python中argv的使用_python sys.argv[]用法

    sys.argv变量是一个字符串的列表.特别地,sys.argv包含了命令行参数 的列表,即使用命令行传递给你的程序的参数. 这里,当我们执行python using_sys.py we are ar ...

  8. python中ln怎么写_Python Decimal ln()用法及代码示例

    Decimal#ln():ln()是一个Decimal类方法,它返回Decimal值的自然(对数e)对数. 用法:Decimal.ln() 参数:十进制值 返回:十进制值的自然(以e为底)对数. 代码 ...

  9. python中tan怎么表示_Python numpy.tan()用法及代码示例

    numpy.tan(array [,out])= ufunc'tan'):此数学函数可帮助用户计算所有x(作为数组元素)的三角切线. 参数: array :[array_like]elements a ...

最新文章

  1. 回文数:给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
  2. ***杂记-2013-07-12
  3. php 操作 redis 常用命令
  4. Google Dremel数据模型详解(下)
  5. java工程师需要的技能_Java工程师需要掌握哪些技能?
  6. Velocity引擎导致jvm内存外内存泄露
  7. mysql 创建数据库文件_mysql学习之通过文件创建数据库以及添加数据
  8. 抱歉(HDU-1418)
  9. ctypes安装_用python amp; bat写软件安装脚本 + HM NIS Edit自动生成软件安装脚本
  10. mybatis源码学习方式
  11. 批量测试Mybatis项目中Sql是否正确
  12. 自由移动的气泡_MBBR工艺—移动床生物膜反应器
  13. 简单谈谈对软件工程的理解
  14. 什么是SWFObject?
  15. 单龙芯3A3000-7A1000PMON研究学习-(28)撸起袖子干-再来一杯代码10-内存初始化1
  16. 4.22 使用裁切命令裁剪图片 [原创Ps教程]
  17. 计算机组成原理课设题目详解
  18. 404报错:The origin server did not find a current representation for the target resource解决的两种办法
  19. mysql 别名排序没效果_除了会排序,你对ORDER BY的用法可能一无所知!
  20. 用sql查询姓名和身份证_查询,更新和身份

热门文章

  1. 组合数学(一):绪论、抽屉原理
  2. 用Word编程?可笑?谁说不可以!!!
  3. 你知道百度的“框计算” 吗
  4. 基于matlab的人脸识别算法
  5. android颜色Color
  6. python标签整理 微信_Python实现清理微信僵尸粉功能示例【基于itchat模块】
  7. cs1.5最新服务器地址,最新cs1.5战网服务器IP
  8. 深入理解WPF框架下的await
  9. JAVA开发的23种设计模式之 —  装饰器模式 代理器模式
  10. CSP-J/S第一轮测试知识点全覆盖