Python Strings Tutorial: Slicing, f-strings and String Methods with Examples
P3.1 How to Create and Join Strings in Python
Why. Reports need labels and messages, like "Branch: Leeds". You build them by joining text.
What. A string is text inside single or double quotes. + joins strings. * repeats them. len() counts characters.
How.
Build a label
- Store each piece of text in a variable.
- Join them with
+, adding spaces as text. - Print the result.
- Use
len()if you need the length.
Worked example A — Create two strings.
Step 1 — What we do. We store a customer name and city.
Step 2 — Type and run this code.
name = "Priya"
city = "Nagpur"
print(name)
print(city)
Step 3 — Output you should see.
Priya
Nagpur
Step 4 — What it means. Both are str values. Quotes are not printed; they only mark where text starts and ends.
Worked example B — Join strings.
Step 1 — What we do. We make one label from the two pieces.
Step 2 — Type and run this code.
name = "Priya"
city = "Nagpur"
label = name + " from " + city
print(label)
Step 3 — Output you should see.
Priya from Nagpur
Step 4 — What it means. The spaces must be inside the quotes, otherwise the words stick together.
Worked example C — Length and repeat.
Step 1 — What we do. We check the length of a product code and print a divider line.
Step 2 — Type and run this code.
code = "SKU-1042"
print(len(code))
print("-" * 20)
Step 3 — Output you should see.
8
--------------------
Step 4 — What it means. The code has 8 characters, including the dash. "-" * 20 repeats the dash 20 times — handy for simple text reports.
Ravindra Bagale's Tip
Keep the original text column untouched and make a cleaned copy. If cleaning goes wrong, you still have the original.
Ravindra Bagale's Tip – मराठी
Original text column तसाच ठेवा आणि साफ केलेली copy बनवा. Cleaning चुकलं तरी original तुमच्याकडे राहतो.
Ravindra Bagale's Tip – हिंदी
Original text column वैसा ही रखो और साफ़ की हुई copy बनाओ. Cleaning गलत हुई तो भी original आपके पास रहेगा.