HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›Strings

Strings

Creating strings, concatenation, length, upper/lowercase, splitting, searching, and formatting with f-strings.

Simple enough for a 5-year-old to understand

Creating and concatenating strings

Strings are written inside single (' ') or double (" ") quotes. They're joined with +.

Example code
1
name = "Alex"
A string.
VariableA string.
2
surname = "Lovelace"
Another string.
VariableAnother string.
3
print(name + " " + surname)
"Alex Lovelace" — we added a space between them.
Output / Input"Alex Lovelace" — we added a space between them.
VariableOutput / Input
💡 Hint: Text + number raises an ERROR. Convert the number with str(), or use an f-string.

Length and letter case

Example code
1
word = "Python"
A 6-letter string.
VariableA 6-letter string.
2
print(len(word))
len = length: writes 6.
Output / Inputlen = length: writes 6.
3
print(word.upper())
PYTHON — uppercase.
Output / InputPYTHON — uppercase.
4
print(word.lower())
python — lowercase.
Output / Inputpython — lowercase.
VariableOutput / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the word "hexlibrary" in UPPERCASE to the screen.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

Text is the data type most frequently dealt with in programming: usernames, file contents, web pages, log records. In Python, text is a string of characters and you can access each character of this string in order; you can slice, concatenate, search, and transform text.

It is important to know that Python strings are IMMUTABLE. When you call a method like upper or replace, the original text does not change; a new text is produced. Therefore, forgetting to assign the result to a variable is a very common mistake.

What you will learn

  • Learning how to create text and find its length with len
  • Extracting a single character with indexing and a part with slicing
  • Using the upper, lower, strip, replace, and split methods
  • Searching within text using the in operator
  • Formatting text with f-strings

When to use?

Text operations appear in almost every program: cleaning leading and trailing spaces from user input, converting an email address to lowercase and comparing it, splitting a comma-separated line into pieces, checking the extension of a filename.

Most data cleaning tasks are done with the quartet of strip, lower, replace, and split.

Common mistakes

Wrong

name = " Ada "
name.strip()
print(name)

strip does not change the original text; it returns a cleaned NEW text. Since you didn't assign the result, name still has spaces and you won't see any difference on the screen.

Correct

name = " Ada "
name = name.strip()
print(name)

Wrong

text = "Python"
print(text[6])

Indices start from zero, meaning the valid indices of a 6-letter text are between 0 and 5. When you ask for 6, you get an "IndexError: string index out of range" error. You can use -1 for the last character.

Correct

text = "Python"
print(text[-1])

Wrong

text = "Python"
text[0] = "J"

Strings are immutable; trying to update a single character in place gives a "TypeError: 'str' object does not support item assignment" error. To change it, you need to generate a new text.

Correct

text = "Python"
text = "J" + text[1:]

Frequently asked questions

How do I find the length of a text?

With the len function. The point to watch out for is that len gives the character count — in texts containing Turkish characters or emojis, it might not be the same number you expect, because some characters consist of multiple code units.

How does slicing work?

You provide a start and end index with a colon. Important rule: start is INCLUSIVE, end is EXCLUSIVE. That means a slice from 0 to 3 gives the first three characters. If you omit the start, it takes from the beginning; if you omit the end, it takes to the end.

How do I compare two texts?

With double equals. However, when comparing user input, you should first clean spaces with strip and convert to lowercase with lower; otherwise, "Ada" and "ada " are counted as different and input validations fail unexpectedly.

Summary

In Python, texts are sequences of characters, can be sliced with indices and slices, and can be transformed with a rich set of methods. The most critical rule is that strings are immutable: every method returns a new text, and you must not forget to assign the result.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code