Python Forum
string ending with other string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string ending with other string
#1
I have to write a program that inputs a pair of string from user. The program should dispaly an appropriate message if either one of the strings ends with the other string. The case of the input
strings does not matter. The program should continue to enter pairs of strings until the user
enters an empty string for either.
Here are sample run:
Enter first string: Hello World

Enter second string: world
String 2 is at the end of String 1

Enter first string: abc

Enter second string: defABC
String 1 is at the end of String 2

Enter first string: hello world
Enter second string: hello

String 1 and String 2 do not end with the other

Enter first string: Hello World again
Enter second string:
second string is empty, goodbye.

Here is my code, program should exit when I put exit when I enter an empty input, butmy code fails to do it, where is my mistake?
f = str(input('Enter first string:'))
s = str(input('Enter second string:'))
while ((len(f) or len(s)) != 0) and (f[0:len(f)] == s[len(s)-(len(f)):len(s)]):
    print('String 1 is at the end of String 2')
    f = input('Enter first string:')
    s = input('Enter second string:')
    while ((len(f) or len(s)) != 0) and (s[0:len(s)] == f[len(f)-(len(s)):len(f)]):
        print('String 2 is at the end of String 1')
        f = input('Enter first string:')
        s = input('Enter second string:')
        while ((len(f) or len(s)) != 0) and (s not in f[len(f)-len(s):len(f)] and f not in s[len(s)-len(f):len(s)]):
            print('String 1 and String 2 do not end with the other')
            f = input('Enter first string:')
            s = input('Enter second string:')
else:
    print('Second string is empty, goodbye.')
Reply
#2
Why do you have three while loops? You should have one while loop. You should check for string at the end of other strings with a conditional (if/elif/else). Exit out of the loop with a break statement if the output is empty.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  string to float conversion fails PetarPetrenko 10 934 Feb-29-2024, 04:20 PM
Last Post: deanhystad
  Help with extracting characters from string valentino1337 2 431 Feb-19-2024, 01:17 PM
Last Post: Pedroski55
  String to Number in Python Godjuned 3 1,345 Nov-17-2022, 07:22 AM
Last Post: Godjuned
  number to string Ali_ 1 1,268 Mar-31-2022, 11:22 AM
Last Post: ndc85430
  position of shortest string Ali_ 2 1,466 Mar-17-2022, 03:48 PM
Last Post: DeaD_EyE
  Count occurences in a string and add to a list using loops Leyo 4 1,668 Mar-11-2022, 03:52 PM
Last Post: Leyo
  Finding how many times substring is in a string using re module ranbarr 4 2,943 May-21-2021, 06:14 PM
Last Post: nilamo
  Checking if string starts the same but end differently using re module ranbarr 1 1,668 May-20-2021, 06:23 PM
Last Post: Gribouillis
  I need help writing this code for string properties and methods hannah71605 4 3,097 Mar-22-2021, 12:36 PM
Last Post: menator01
  Searching for parts of a string or word Ulumulu 8 3,710 Mar-15-2021, 07:02 PM
Last Post: Ulumulu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020