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