Console Input and Formatting
Lesson 27 of 48 in Coddy's Python for Kotlin Developers course.
Kotlin readln and Python input return a line of text without its line terminator. Parse numeric input explicitly. Python strip removes surrounding whitespace; internal spaces remain. F-strings can format decimal places, analogous to JVM String.format with a chosen locale. The task's exact stdout contract includes spaces and final newlines, so do not add prompts or debug output.
Kotlin
import java.util.Locale
fun main() {
val label = " Green tea ".trim()
val cents = 305
println(String.format(Locale.ROOT, "%s: %.2f", label, cents / 100.0))
}Python
label = " Green tea ".strip()
cents = 305
print(f"{label}: {cents / 100:.2f}")Porting trap
These tasks are command-line programs, not Android UI callbacks. The locked driver controls input and output for function tasks. Return the result rather than printing from a helper.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
Challenge
BeginnerRead a product label on the first line and a nonnegative integer number of cents on the second. Remove surrounding whitespace from the label but preserve internal spaces. Print exactly label: dollars.cents, with two digits after the decimal point and a final newline. The label may be empty, in which case the output begins with : . Do not print a currency symbol or input prompts.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Kotlin Developers
1From Kotlin to Python
Run Your First Python ProgramPrint and String TemplatesVal, Var, and Python NamesDivision and Integer WidthSuites and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and Loop BindingsEntries and withIndexUpdates and Loop ControlCollection PipelinesRecap: Build a Ranking2Values and Decisions
Nullable Types and NoneEquality and IdentityBoolean Conditions and orIf Expressions and ElifRecap: Eligibility Rules3Collections and Shared State
Read-only Lists and MutationPair and Tuple UnpackingMap Lookup and Missing KeysShallow Copies and SetsRecap: Clean an Inventory6Input, Errors, and Modules
Console Input and FormattingExceptions and Resource ScopeImports and Entry PointsStatic Types and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler