当前位置:网站首页>字符串最后一个单词的长度
字符串最后一个单词的长度
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
边栏推荐
- Recommended search common evaluation indicators
- 什么是CNAS认证?CNAS认可的软件测评中心有哪些?
- fatal error: torch/extension.h: No such file or directory
- 移动金融(自用)
- Introduction to dynamic programming of leetcode learning plan day3 (198213740)
- adobe illustrator 菜单中英文对照
- Error: unable to find remote key "17f718f726"“
- T2 icloud calendar cannot be synchronized
- JSON date time date format
- 自主作业智慧农场创新论坛
猜你喜欢

Sorting and replying to questions related to transformer

Detailed explanation of MySQL connection query

服务器中毒了怎么办?服务器怎么防止病毒入侵?

Analysis of common storage types and FTP active and passive modes

群体智能自主作业智慧农场项目启动及实施方案论证会议

setcontext getcontext makecontext swapcontext

Multitimer V2 reconstruction version | an infinitely scalable software timer

Sword finger offer (2) -- for Huawei

Detailed explanation of kubernetes (XI) -- label and label selector

cadence SPB17.4 - Active Class and Subclass
随机推荐
Krpano panorama vtour folder and tour
电脑怎么重装系统后显示器没有信号了
Connectez PHP à MySQL via aodbc
fatal error: torch/extension.h: No such file or directory
kubernetes之常用Pod控制器的使用
Crawling fragment of a button style on a website
T2 iCloud日历无法同步
[leetcode daily question] install fence
Node. JS ODBC connection PostgreSQL
PHP operators
Modify the default listening IP of firebase emulators
Grep was unable to redirect to the file
What are the mobile app software testing tools? Sharing of third-party software evaluation
MySQL installation process (steps for successful installation)
T2 icloud calendar cannot be synchronized
utils.DeprecatedIn35 因升级可能取消,该如何办
Use of common pod controller of kubernetes
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
软件性能测试报告起着什么作用?第三方测试报告如何收费?
自动化测试框架常见类型▏自动化测试就交给软件测评机构