Home / Python Tutorial / Day 3
Day 3 — Python strings and f-strings
Most real data arrives as messy text. Today you learn to cut it, clean it and format it.
Strings are sequences
Each character has a position, counting from 0.
Slicing
[start:stop] — the start is included, the stop is not.
Why stop is excluded
It looks odd until you notice
word[0:4] plus word[4:] gives you back the whole word, with nothing repeated or missed.The string methods you will actually use
Cleaning up a messy export is usually just .strip() and .title().
split() and join()
replace() and length
f-strings — the modern way to build text
Put f before the quote, then drop any variable inside { }.
You can do maths inside the braces, and control decimal places with :.2f.
You cannot add text to a number
"Total: " + 500 is an error. Either use an f-string, or wrap the number: "Total: " + str(500). An f-string is almost always nicer.Try these yourself
- Take the string " data analytics " and print it stripped and in title case.
- Split "Mon,Tue,Wed" on the comma and print the second day.
- Use an f-string to print "Amit earned 42000" from two variables.
- Print 2/3 to four decimal places.
Ready for pandas and real projects?
This tutorial covers core Python. Our Data Analytics course takes you into pandas, NumPy, real files and visualisation, with mentor support and portfolio projects.
See the course →