Python Forum
Using .format() with expresions.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using .format() with expresions.
#1
I am new to Python and have been trying to trim some code for brevity.
Following is the original code.
user_sentance = input("Enter a sentance. ")
user_character = input("Enter a character. ")
index = user_sentance.find(user_character)
print("{} is at index {}.".format(user_character, index))
Then I try to replace the print() statement with:
print("{} is at index {}.".format(user_character, user_sentance.find(user_character))
I get an invaled syntax error.
Why does this not work? What am I missing?

Python 3.8
macOS Catalina
Reply
#2
You are missing a close parenthesis at the end of the line.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
f-string is better yet:
print(f"{user_character} is at index {index}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with regular expresions-SOLVED dragan979 3 2,555 Jan-10-2019, 03:06 PM
Last Post: dragan979

Forum Jump:

User Panel Messages

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