python中isalpha的用法_Python string isalpha()用法及代码示例
在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()用法及代码示例相关推荐
- python isnumeric函数用法_Python string isnumeric()用法及代码示例
在Python中,isnumeric()是用于字符串处理的内置方法.如果字符串中的所有字符均为数字字符,则issnumeric()方法返回"True",否则,返回"Fal ...
- python中count的作用_python count函数用法详解
在python中可以使用"count()"函数统计字符串里某个字符出现的次数,该函数用于统计次数,其语法是"count(sub, start= 0,end=len(str ...
- python中图例legend标签内容_matplotlib设置legend图例代码示例
matplotlib设置legend图例代码示例 本文主要是关于matplotlib的一些基本用法. Demo import matplotlib.pyplot as plt import numpy ...
- python画折线图虚线_python绘制简单折线图代码示例
1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...
- python中的get函数_python之函数用法get()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法get() #http://www.runoob.com/python/att-dic ...
- python中sinh是什么_Python PyTorch sinh()用法及代码示例
PyTorch是由Facebook开发的开源机器学习库.它用于深度神经网络和自然语言处理. 功能torch.sinh()为PyTorch中的双曲正弦函数提供支持.它期望以弧度形式输入.输入类型为张量, ...
- python中argv的使用_python sys.argv[]用法
sys.argv变量是一个字符串的列表.特别地,sys.argv包含了命令行参数 的列表,即使用命令行传递给你的程序的参数. 这里,当我们执行python using_sys.py we are ar ...
- python中ln怎么写_Python Decimal ln()用法及代码示例
Decimal#ln():ln()是一个Decimal类方法,它返回Decimal值的自然(对数e)对数. 用法:Decimal.ln() 参数:十进制值 返回:十进制值的自然(以e为底)对数. 代码 ...
- python中tan怎么表示_Python numpy.tan()用法及代码示例
numpy.tan(array [,out])= ufunc'tan'):此数学函数可帮助用户计算所有x(作为数组元素)的三角切线. 参数: array :[array_like]elements a ...
最新文章
- 回文数:给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
- ***杂记-2013-07-12
- php 操作 redis 常用命令
- Google Dremel数据模型详解(下)
- java工程师需要的技能_Java工程师需要掌握哪些技能?
- Velocity引擎导致jvm内存外内存泄露
- mysql 创建数据库文件_mysql学习之通过文件创建数据库以及添加数据
- 抱歉(HDU-1418)
- ctypes安装_用python amp; bat写软件安装脚本 + HM NIS Edit自动生成软件安装脚本
- mybatis源码学习方式
- 批量测试Mybatis项目中Sql是否正确
- 自由移动的气泡_MBBR工艺—移动床生物膜反应器
- 简单谈谈对软件工程的理解
- 什么是SWFObject?
- 单龙芯3A3000-7A1000PMON研究学习-(28)撸起袖子干-再来一杯代码10-内存初始化1
- 4.22 使用裁切命令裁剪图片 [原创Ps教程]
- 计算机组成原理课设题目详解
- 404报错:The origin server did not find a current representation for the target resource解决的两种办法
- mysql 别名排序没效果_除了会排序,你对ORDER BY的用法可能一无所知!
- 用sql查询姓名和身份证_查询,更新和身份