
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to find the maximum length of a string in a given Series
Input β
Assume, we have a Series like this, [βoneβ, βtwoβ, βelevenβ, βpomegranatesβ, βthreeβ] and the maximum length of the string is βPomegranatesβ
Solution
To solve this, we will follow the below approaches.
Define a Series
Set the initial value of a maxlen is 0
Set the βmaxstrβ value is initially empty string.
Create a for loop and access all the values in the Series one by one and create an if condition to compare the value based on the length as follows β
for i in res: if(len(i)>maxlen): maxlen = len(i) maxstr = i
Finally, print the value stored in the βmaxstrβ variable.
Example
Let us see the following implementation to get a better understanding.
import pandas as pd res = pd.Series(["one","two","eleven","pomegranates","three"]) maxlen = len(res[0]) maxstr = "" for i in res: if(len(i)>maxlen): maxlen = len(i) maxstr = i print(maxstr)
Output
pomegranates
Advertisements