Python Forum
Printing a string from a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing a string from a variable
#1
I have a program that I need to make where I ask for the user's city and state, and ask them to type it in this format: city,state. The city must be printed on one line and the state just below. However, I am confused on how to accomplish this, because obviously the variable will be different among users and the index locations will change. Any help is appreciated.

This is what I have so far:
def main():
  # ask user for their city and staticmethod
  userInput = input("Please enter your city and state in the form of city,state")
  
  print("You live in the state of ",userInput[0, ","])
  

main()
Larz60+ write Nov-07-2020, 04:32 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Added for you this time, please use bbcode tags in all future posts.
Reply
#2
Have you tried using split to separate City and State? It worked for me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using an integer to manipulate a string/text variable rexyboy2121 1 1,868 Apr-22-2020, 01:37 AM
Last Post: michael1789
  Issues with printing a variable fad3r 1 2,255 May-03-2018, 06:47 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

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