当前位置:网站首页>字符串最后一个单词的长度
字符串最后一个单词的长度
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
边栏推荐
- cadence SPB17. 4 - Active Class and Subclass
- Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
- 群体智能自主作业智慧农场项目启动及实施方案论证会议
- Independent operation smart farm Innovation Forum
- Openstack command operation
- 通过 PDO ODBC 将 PHP 连接到 MySQL
- 控制结构(一)
- Node.js ODBC连接PostgreSQL
- Kubernetes详解(九)——资源配置清单创建Pod实战
- 软件性能测试报告起着什么作用?第三方测试报告如何收费?
猜你喜欢

Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project

2022年中国数字科技专题分析

ICE -- 源码分析

推荐搜索 常用评价指标

Sorting and replying to questions related to transformer

考试考试自用

Today's sleep quality record 76 points

Independent operation smart farm Innovation Forum

What if the server is poisoned? How does the server prevent virus intrusion?

My raspberry PI zero 2W toss notes to record some problems and solutions
随机推荐
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
MySQL query library size
Summary of interfaces for JDBC and servlet to write CRUD
Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
Node. JS ODBC connection PostgreSQL
推荐搜索 常用评价指标
redis-shake 使用中遇到的错误整理
Hj31 word inversion
CVPR 2022 优质论文分享
网站建设与管理的基本概念
PHP 的运算符
php类与对象
木木一路走好呀
编译,连接 -- 笔记
tcp_ Diag kernel related implementation 1 call hierarchy
Deep learning - Super parameter setting
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
Today's sleep quality record 76 points
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
Analysis of common storage types and FTP active and passive modes