Count letters after a specific word

Question:

I have this response from a server: HTTP-Version: WEB 981.20rnrnlogintype=none&since=today

I know you can count letters by using the function len() but this function will count all letters of the response.

How can I count the letters after this phrase: HTTP-Version: WEB 981.20rnrn?

The result should be 26.

Asked By: Ali Ahmed

||

Answers:

Try out this python code:

response = "HTTP-Version: WEB 981.20rnrnlogintype=none&since=today"
substring = response.split("HTTP-Version: WEB 981.20rnrn")[1]
print(substring)
count=len(substring)
print(count)

This code just grabs whatever is after the string you provided and puts it into a substring, then counts it

Answered By: Max

this problem had many solution but this is the easiest one:

response = "HTTP-Version: WEB 981.20rnrnlogintype=none&since=today"

and then:

part_response = response[-26:]
print(len(part_reponse))
Answered By: ARYAN-NIKNEZHAD

In order to do this in a robust manner you need to do several things.

First of all you need to check if the phrase exists in the response and furthermore where it exists.

If it does exist, you can then get the length of that part of the response that comes after the phrase.

As follows:

response = 'HTTP-Version: WEB 981.20rnrnlogintype=none&since=today'
phrase = 'HTTP-Version: WEB 981.20rnrn'

if (offset := response.find(phrase)) >= 0:
    print(len(response[offset+len(phrase):]))

Output:

26
Answered By: DarkKnight
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.