当前位置:网站首页>字符串最后一个单词的长度
字符串最后一个单词的长度
2022-04-23 15:34:00 【叶宁夏昕】
一、计算字符串最后一个单词的长度
题目描述:计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
输入描述:
输入一行,代表要计算的字符串,非空,长度小于5000。
输出描述:
输出一个整数,表示输入字符串最后一个单词的长度。
题目来源:牛客网(华为机试)
解题思路:
1.获取输入字符串,用input()
2.用if函数判断字符串的长度小于且不为空
3.用strip(’ ')去除左右两边可能输入的空格
4.使用字符串转列表函数split(指定分割字符)将字符串以空格分割为列表
5.获取列表最后一个元素的长度,从第一个字符开始下标为0,依次往后,从最后一个字符开始,下标为-1,依次减小,-2,-3.。。。
words = input()
if len(words) <= 5000 and words is not None:
words = words.strip(' ')
word_list = words.split(' ')
print(len(word_list[-1]))
二、python strip()方法使用
python strip()方法使用_liff_lee的博客-CSDN博客_strip方法
三、split()函数介绍
版权声明
本文为[叶宁夏昕]所创,转载请带上原文链接,感谢
https://blog.csdn.net/come_bn/article/details/124270896
边栏推荐
- 今日睡眠质量记录76分
- About UDP receiving ICMP port unreachable
- cadence SPB17. 4 - Active Class and Subclass
- Hj31 word inversion
- What if the server is poisoned? How does the server prevent virus intrusion?
- The El tree implementation only displays a certain level of check boxes and selects radio
- Mysql database explanation (IX)
- Recommended search common evaluation indicators
- Mysql database explanation (8)
- adobe illustrator 菜单中英文对照
猜你喜欢
Kubernetes详解(九)——资源配置清单创建Pod实战
API gateway / API gateway (II) - use of Kong - load balancing
Functions (Part I)
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
setcontext getcontext makecontext swapcontext
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
机器学习——逻辑回归
Sorting and replying to questions related to transformer
JVM-第2章-类加载子系统(Class Loader Subsystem)
木木一路走好呀
随机推荐
PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
Squid agent
X509 certificate cer format to PEM format
调度系统使用注意事项
Explanation 2 of redis database (redis high availability, persistence and performance management)
函数(第一部分)
MySQL sync could not find first log file name in binary log index file error
Elk installation
T2 icloud calendar cannot be synchronized
Crawling fragment of a button style on a website
MySQL InnoDB transaction
Collation of errors encountered in the use of redis shake
pywintypes.com_error: (-2147221020, ‘无效的语法‘, None, None)
PHP 的运算符
cadence SPB17. 4 - Active Class and Subclass
今日睡眠质量记录76分
Deep learning - Super parameter setting
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
Sword finger offer (1) -- for Huawei