在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. [Js]删除数组指定元素
  2. [转载zz] Python3 输入和输出之序列化与反序列化
  3. 本周学习进度表及时间安排(2018-1-7~2018-1-13)
  4. ios android 录音格式,跨系统的录音格式兼容性问题: iOS Android
  5. Poj 2284 That Nice Euler Circuit
  6. Winform中对ZedGraph的曲线标签进行设置,比如去掉标签边框
  7. 【CSS】学习笔记1 使用CSS样式表
  8. 启明星会议室预定系统 helpdesk系统等 登陆失败的问题与解决方法
  9. CMake交叉编译配置
  10. PHP Fatal error: Declaration of Hyperf\Framework\SymfonyEventDispatcher::dispatch($event) must be c
  11. 软件测试必问必背面试题
  12. 如何去除 WinRAR 的弹窗广告
  13. VUE前端+Node后台模拟打印机Web即时打印
  14. QPA(Qt Platform Abstraction)介绍 --------QWS(Qt Window System)介绍
  15. Code Smell 检测工具调研
  16. java爬虫系列(三)——漫画网站爬取实战
  17. 电脑无法连接wifi得解决方法
  18. python tkinter画布设置按钮对图片放大缩小_Tkinter可调整大小的对象Python画布
  19. Camtasia Studio 6录制视频时鼠标闪烁的解决办法
  20. HDU-5976 Detachment

热门文章

  1. openjudge1.7.13
  2. Vue3滚动加载(懒加载)
  3. 练习题005:冒泡排序
  4. 客服人员如何应对难缠的顾客
  5. oracle 谓词推入失效,oracle view 谓词推入
  6. 拨测API接口+监控方案
  7. Python全栈(八)Flask项目实战之10.前台发布帖子和后台帖子管理页面搭建
  8. 宝马集团签约远景动力为美国工厂供应电芯
  9. Java对比俩个集合的异同
  10. java dwr 漏洞_实战渗透-基于DWR框架下的漏洞探测