当前位置:网站首页>字符串最后一个单词的长度
字符串最后一个单词的长度
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
边栏推荐
- 软件性能测试报告起着什么作用?第三方测试报告如何收费?
- pgpool-II 4.3 中文手册 - 入门教程
- Special analysis of China's digital technology in 2022
- Connectez PHP à MySQL via aodbc
- PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
- Mobile finance (for personal use)
- regular expression
- Openstack theoretical knowledge
- PHP PDO ODBC将一个文件夹的文件装载到MySQL数据库BLOB列,并将BLOB列下载到另一个文件夹
- cadence SPB17.4 - Active Class and Subclass
猜你喜欢
Wechat applet customer service access to send and receive messages
How to design a good API interface?
Detailed explanation of kubernetes (XI) -- label and label selector
c语言---指针进阶
Basic concepts of website construction and management
Mysql database explanation (VII)
今日睡眠质量记录76分
电脑怎么重装系统后显示器没有信号了
Mysql database explanation (8)
网站压测工具Apache-ab,webbench,Apache-Jemeter
随机推荐
移动app软件测试工具有哪些?第三方软件测评小编分享
Mysql连接查询详解
Grep was unable to redirect to the file
Sword finger offer (2) -- for Huawei
怎么看基金是不是reits,通过银行购买基金安全吗
What is CNAs certification? What are the software evaluation centers recognized by CNAs?
Detailed explanation of MySQL connection query
函数(第一部分)
The wechat applet optimizes the native request through the promise of ES6
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
Modify the default listening IP of firebase emulators
Sorting and replying to questions related to transformer
php函数
Node.js ODBC连接PostgreSQL
推荐搜索 常用评价指标
Summary of interfaces for JDBC and servlet to write CRUD
Do keyword search, duplicate keyword search, or do not match
Deep learning - Super parameter setting
Code live collection ▏ software test report template Fan Wen is here