# Python | Longest Run of given Character in String

Sometimes, while working with Strings, we can have a problem in which we need to perform the extraction of length of longest consecution of certain letter. This can have application in web development and competitive programming. Lets discuss certain ways in which this task can be performed.

Method #1 : Using loop
This is brute force method in which we can perform this task. In this, we run a loop over the String and keep on memorizing the maximum whenever the run occurs.

 `# Python3 code to demonstrate working of  ` `# Longest Run of Character in String ` `# Using loop ` ` `  `# initializing string ` `test_str ``=` `'geeksforgeeeks'` ` `  `# printing original string ` `print``(``"The original string is : "` `+` `test_str) ` ` `  `# initializing K  ` `K ``=` `'e'` ` `  `# Longest Run of Character in String ` `# Using loop ` `res ``=` `0` `cnt ``=` `0` `for` `chr` `in` `test_str: ` `    ``if` `chr` `=``=` `K: ` `        ``cnt ``+``=` `1` `    ``else``: ` `        ``res ``=` `max``(res, cnt) ` `        ``cnt ``=` `0` `res ``=` `max``(res, cnt) ` ` `  `# printing result  ` `print``(``"Longest Run length of K : "` `+` `str``(res))  `

Output :

```The original string is : geeksforgeeeks
Longest Run length of K : 3
```

Method #2 : Using `max() + re.findall()`

This is one liner way in which this problem can be solved. In this, we find the maximum of all runs found using findall().

 `# Python3 code to demonstrate working of  ` `# Longest Run of Character in String ` `# Using max() + re.findall() ` `import` `re ` ` `  `# initializing string ` `test_str ``=` `'geeksforgeeeks'` ` `  `# printing original string ` `print``(``"The original string is : "` `+` `test_str) ` ` `  `# initializing K  ` `K ``=` `'e'` ` `  `# Longest Run of Character in String ` `# Using max() + re.findall() ` `res ``=` `len``(``max``(re.findall(K ``+` `'+'``, test_str), key ``=` `len``)) ` ` `  `# printing result  ` `print``(``"Longest Run length of K : "` `+` `str``(res))  `

Output :

```The original string is : geeksforgeeeks
Longest Run length of K : 3
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.