Question

Python 3 Code: String #1 = Denver, CO, USA String #2 = Denver, CO How would...

Python 3 Code:

String #1 = Denver, CO, USA

String #2 = Denver, CO

How would I index to print the string that comes after the second comma? I would like to only print "USA". for string #1, and get an error for string#2.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In this Python program,
- We have used the concept of split() in order to split the string into a list based on a delimiter.
- Then finally printing the string occurred after the 2nd comma using the list index

Program:

#Defined 2 strings

String1 = "Denver, CO, USA"
String2 = "Denver, CO"

#Splitting the string using, as separator and store it in a list
#(since there are spaces too in the string I also included the space )
x1 = String1.split(", ")
x2 = String2.split(", ")

#Inorder to print the string after the 2nd comma Just use index to do so

print(x1[2])
print(x2[2])

main.py 1 #Defined 2 strings 2 3 Stringi = Denver, co, USA 4 String2 Denver, CO 5 6 #Splitting the string using , as sepe

Output:
USA Traceback (most recent call last): File main.py, line 14, in <module> print (x2[2]) IndexError: list index out of range

Hope the answer is clear and Helps!!!
Please upvote as well, If you got the answer ?
If not please comment, I will Help you with that...

Add a comment
Know the answer?
Add Answer to:
Python 3 Code: String #1 = Denver, CO, USA String #2 = Denver, CO How would...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code is wrong: python parsing warm up. while(True): inp_str = input('Enter input string: ') if (inp_str...

    Code is wrong: python parsing warm up. while(True): inp_str = input('Enter input string: ') if (inp_str == 'q'): break; while inp_str.find(",")==-1: print('Error: No comma in string. ') inp_str=input('Enter input string: ')       s=inp_str.split(",") print('First word:'+ s[0].strip()) print('Second word:'+ s[1].strip()) print('\n') This is my code for a problem but the output is wrong and I dont know how to fix it (the top is the output im getting the bottom is what i am supposed to get) Enter input string:...

  • Questin 1 (CO 2) Which of the following is not a valid name in python? last_name...

    Questin 1 (CO 2) Which of the following is not a valid name in python? last_name lastName last name lname Question 2 (CO 3) How many times will “Hello World” be displayed after the following code executes? num=2 while num<12:     print("Hello world")     num+=2 2 12 5 4 Question 3 (CO 1) The following code contains an error, what type of error is it and what line number is it? 1 count=1 2 while count<4 3    print("count = ",...

  • Python QUESTION A Below we have an assignment of a string to s. What is the...

    Python QUESTION A Below we have an assignment of a string to s. What is the index of the colon in s? You can use find method in Python to get the answer. The answer should be an integer. s = 'X-DSPAM-Confidence: 0.8475' __________ QUESTION B What did you type to get the answer above? State the exact Python code (codes like s.split(), len(s), print('hello')). ___________ QUESTION C Now extract only the substring 0.8475 from s with expression s[ ]....

  • 2 of these blocks of code will produce an error if run. Find each statement with an error in it, ...

    2 of these blocks of code will produce an error if run. Find each statement with an error in it, describe the error and what behavior it will produce, and describe generally how you would fix it. print ("block 1" strl-"INST 126 is my first Python course" print (strl[-6:len Thu, Apr 11, 2019 9:38 PM it 6 characters of the string list1 = str1 . split() # split into words print (list 1 [len (list 1))) # get last word...

  • 6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains...

    6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two...

  • can anyone show me how this code would look in python, I'm studying python while taking...

    can anyone show me how this code would look in python, I'm studying python while taking some classes. I can't seem to understand what this is asking for. • Phase 1 Requirements Code submitted for Phase 1 will only have the following capability. Any additional coding will result in the loss of point. • code will have the main function, including looping until the user enters input of 12 • code will call the printMenu() function • The printMenu() function...

  • Select all true statements about Python string indexing: Python uses indexing operator (square br...

    Select all true statements about Python string indexing: Python uses indexing operator (square brackets) to select a single character from a string Python supports indexing in both left to right and right to left Left to right indexing starts with 0 Right to left indexing starts with -1 The return type of a string index is also a string Select all the ways you can extract "rocks" out of s = "Python rocks": print(s[7:]) print(s[7:20]) print(s[-5:]) print(s[-5::1]) print(s[-5::]) Given the...

  • What would be the output of the following code? def get_letter_index(letter, string): returns the index of...

    What would be the output of the following code? def get_letter_index(letter, string): returns the index of letter in string, OR if letter is not found in string, prints that information instead. If the letter occurs multiple times, this returns only the first index where the letter is found. Index where the letter is found for i in range(len(string)): if string[i] == letter: return i #if execution reaches this point, we must not have found the letter print(letter, "not found in",...

  • Hi I'm trying to write a code for a web server in python with flask. This...

    Hi I'm trying to write a code for a web server in python with flask. This is what I have so far from flask import Flask app = Flask(__name__) @app.route('/') #first endpoint i.e. "http://localhost/" def index(): return 'hello' #return data in string if __name__ == '__main__': app.run(debug=True) After running the code, I'm given a address to the web with the text hello. The problem is that this only works with my computer that is running the code. If I try...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is sued. Write a Python...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is sued. Write a Python code that gets a string containing a person’s first, middle, and last name, and print her/his last name, followed by comma and the first name initial. For example, if the user enters Randy Rick Gomez, the program should display Gomez, R. Please note that in the output, the first character of each name should be capital. Please see the outcomes below: Outcome: Enter your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT