cat, print, and Strings
Lesson 2 of 48 in Coddy's Python for R Developers course.
R distinguishes console representations from plain text output: print often includes an index label and quotes, while cat writes values with chosen separators. Python print writes a readable string representation and ends with a newline by default. Both languages use a hash sign for comments outside strings. Python f-strings embed expressions in braces; an ordinary string preserves the braces. R commonly constructs text with paste or paste0 before emitting it. Single and double quotes are delimiters in both languages, not an automatic interpolation switch. Choose output intentionally rather than copying R console transcripts containing index labels into the expected program output.
R
name <- "Ada"
cat(paste("Hi", name), "\n", sep="")
cat("Hi {name}\n")Python
name = 'Ada'
print(f"Hi {name}")
print("Hi {name}")Porting trap
Use plain output for exact grading. Python print does not reproduce R's printed vector index labels.
Challenge
BeginnerInput is one line of text, which may be empty or contain quotes or #. Print Message:, one space, and that exact text on the first line. Print It's Python on the second line. Remove or comment out the diagnostic print in the starter. input() is supplied here to read one line without its final newline.
Print only the requested output, with a newline after each line. The supplied input lines read the test data; keep them and add your work below.
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 R Developers
1From R to Python
Run Your First Python Programcat, print, and StringsBindings and Copy SemanticsInteger Arithmetic and NABraces Become SuitesRecap: Rewrite a Receipt4Iteration and Transformations
seq_along and rangeitems and enumeratenext and continuelapply and ComprehensionsRecap: Build a Ranking2Values and Decisions
NULL, NA, and NoneVector Equality and IdentityScalar Conditions, Not Vectorsifelse and Scalar SelectionRecap: Eligibility Rules3Vectors to Containers
Vectors, Lists, and Index ZeroTuples and Exact UnpackingNamed Lists and Dictionariesunique and Shallow CopiesRecap: Clean an Inventory6Reliable Python Programs
readLines and Text ContractsConditions and Cleanupsource and Module ImportsAnnotations and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler