当前位置:网站首页>字符串最后一个单词的长度
字符串最后一个单词的长度
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
边栏推荐
- How to design a good API interface?
- Introduction to dynamic programming of leetcode learning plan day3 (198213740)
- 调度系统使用注意事项
- G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
- How to test mobile app?
- The wechat applet optimizes the native request through the promise of ES6
- How did the computer reinstall the system? The display has no signal
- After time judgment of date
- Error: unable to find remote key "17f718f726"“
- Nacos程序连接MySQL8.0+ NullPointerException
猜你喜欢
随机推荐
考试考试自用
About UDP receiving ICMP port unreachable
服务器中毒了怎么办?服务器怎么防止病毒入侵?
移动金融(自用)
What are the mobile app software testing tools? Sharing of third-party software evaluation
Detailed explanation of MySQL connection query
G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
Application of skiplist in leveldb
kubernetes之常用Pod控制器的使用
通过 PDO ODBC 将 PHP 连接到 MSSQL
Knn,Kmeans和GMM
Squid agent
Functions (Part I)
Tun model of flannel principle
Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
Connect PHP to MySQL via PDO ODBC
Adobe Illustrator menu in Chinese and English
Today's sleep quality record 76 points
如果conda找不到想要安装的库怎么办PackagesNotFoundError: The following packages are not available from current
今日睡眠质量记录76分