当前位置:网站首页>字符串最后一个单词的长度
字符串最后一个单词的长度
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
边栏推荐
- The wechat applet optimizes the native request through the promise of ES6
- 考试考试自用
- Crawling fragment of a button style on a website
- Precautions for use of dispatching system
- My raspberry PI zero 2W toss notes to record some problems and solutions
- JVM-第2章-类加载子系统(Class Loader Subsystem)
- Detailed explanation of kubernetes (XI) -- label and label selector
- 今日睡眠质量记录76分
- cadence SPB17.4 - Active Class and Subclass
- Sword finger offer (2) -- for Huawei
猜你喜欢
随机推荐
编译,连接 -- 笔记
T2 icloud calendar cannot be synchronized
群体智能自主作业智慧农场项目启动及实施方案论证会议
Application of skiplist in leveldb
激活函数的优缺点和选择
Deep learning - Super parameter setting
What if the package cannot be found
adobe illustrator 菜单中英文对照
【backtrader源码解析18】yahoo.py 代码注释及解析(枯燥,对代码感兴趣,可以参考)
函数(第一部分)
Today's sleep quality record 76 points
Compiling OpenSSL
Sorting and replying to questions related to transformer
Special analysis of China's digital technology in 2022
pgpool-II 4.3 中文手册 - 入门教程
调度系统使用注意事项
cadence SPB17.4 - Active Class and Subclass
Grep was unable to redirect to the file
Common interview questions of operating system:
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list


![[leetcode daily question] install fence](/img/4e/3ac23174f2b3ee867b45a2e748b872.png)






