Post

Strings Data Types - What Are Strings, How To Use String Methods, and String Interpolation

On Day 4 of the 30DaysOfPython course, I'll be revisiting the topic of String in Python. It should be

Strings Data Types - What Are Strings, How To Use String Methods, and String Interpolation

In today’s module, I’ll be learning all about strings and their formatting and functions available in Python1. We’ll go over many core concepts and frameworks that are available to us through Python and I’ll give as many examples as I can to enhance my own learning.


What are strings?

The best description of a string is as follows; a text is a string data type which is enclosed within single, double, or triple quotation marks. It can contain any type of information but the quotation marks must be used.

To make it a little clearer:

1
2
3
4
5
var_a = 'this is a string'
var_b = "this is a string"
var_c = """this is a string"""
var_d = '''this is a string'''
var_e = """this is a string"""

Strings can be used throughout multiple lines of code also and that’s where you would use the triple quotation marks, which is useful when dealing with larger bits of informations.

1
2
3
4
5
6
7
text = '''
this is over
more than
1 line 
of code'''

# Can be ' or "

Built‑In Functions: Starting With len()

Before I begin here, I would like to add that there are many built-in functions that Python has and I have discussed this in one of my earlier posts - Variables, Built-in Functions, and Data Types in Python.

Here is the official link from Python for built-in functions Python Documentation - Built-In Functions!

1
2
var_a = "My name is Sheikh Hussain"
print(len(var_a))  # 25

When it comes to strings, think of this concept:

Strings are countable, indexable objects — not just text.

Spaces count, every character counts, and Python is keeping track of all of it.

This is just one of the many functions you can do but it is often used as it helps with topics like slicing which we’ll learn later.

Joining Strings Together (String Concatenation)

Concatenating strings could be useful for many reasons, you might need to take a piece of text, have that seperated, then joined back again in different ways.

By this point, it was just useful for me to know we can join strings together.

1
2
3
4
fname = "Sheikh"
sname = "Hussain"
full_name = fname + " " + sname
print(full_name)

Remember:

Using the + doesn’t add strings together, it creates a new string and presents that to you. The original variable stays the same.


Escape Sequences

Escape sequences should be thought of as being very similar to string formatting, which we will go over later.

Essentially, you use a \ (backslash) character to invoke a function at that position.

For example:

1
2
3
4
print("Hello\nWorld")

#Hello
#World

The \n isn’t printed — it tells Python to place what’s after it on a new line.

There are several different types of escape functions that we can use and here is a list:

Escape SequenceMeaningExample CodeOutput
\nNew lineprint("Hello\nWorld")Hello
World
\tTab spaceprint("A\tB")A  B
\\Backslashprint("C:\\path")C:\path
\'Single quoteprint('It\'s fine')It’s fine
\"Double quoteprint("She said \"Hi\"")She said “Hi”
\rCarriage return (overwrites line start)print("Hello\rWorld")World
\bBackspaceprint("ABC\bD")ABD
\fForm feed (page break, often invisible)print("Page1\fPage2")Page1 Page2
\vVertical tabprint("A\vB")A
 B
\aBell / alert soundprint("\a")🔔 (sound, no visible text)
\0Null characterprint("A\0B")A B
\oooOctal valueprint("\141")a
\xhhHex valueprint("\x61")a
\N{name}Unicode by nameprint("\N{COPYRIGHT SIGN}")©
\uXXXXUnicode (16-bit)print("\u03A9")Ω
\UXXXXXXXXUnicode (32-bit)print("\U0001F600")😀

This is a comprehensive list created with AI, I would say only 1/3 of them you’ll want to really use. - It’s just good to know.


String Formatting

This is where strings stopped feeling basic and started feeling useful.

Formatting lets me place values inside strings dynamically — which is exactly what programs need to do.

There are two methods we can use which are a combination of formatting methods taking from Python2 known as Old Style and then found also in Python3.

Old‑Style Formatting (%)

The old style formatting is becoming obselete, however you may come across this as it is not completely rundandant code.

Here, the % symbol acts as a placeholder that marks where a value should be inserted into the string.

That value can be text, an integer, or a floating-point number.

Each % placeholder is filled in order, so the variables you provide afterward must follow the same sequence as the placeholders appear in the string.

1
2
3
4
name = "Sheikh"
language = "Python"
message = "Hi, my name is %s and I love %s" %(name, language)
print(message) # Hi, my name is Sheikh and I love Python

String formats:

Here is a list of the different string formatting we can use:

  • %s - Strings
  • %d = Integers (digits)
  • %f = Floats (decimal numbers)
  • %.(X)f = Specify how many decimals i.e %.2f or %.3f
1
2
3
var_a = 123.123456789
var_b = '%.2f'%(var_a)
print(var_b) # 123.12

New‑Style Formatting (format() and f‑strings)

This method I found to be more efficient and practical when it came to coding with.

Here we use {} as placeholders instead and we use .format() at the end to specify the variables inside in the order we want them appearing.

A more modern way to format strings in Python 3 is by using curly braces {} with the variable to be inserted inside also like: {variable_a}. This approach feels more natural because you don’t need to worry about the order of variables like you do with the older .format() or %() method.

Keep in mind that all the different string formats in the old style formatting are still available in the new style formatting.

1
2
3
4
5
name = "Sheikh"
number = 4325.435435

message = f"Hi, I’m {name} and this number prints to {number:.2f}"
print(message) #Hi, I'm Sheikh and this number prints to 4325.43

This is now my default approach.


Strings as Sequences

This was the biggest mindset change for me.

Strings aren’t just stored text — they are sequences of characters, each within an index position.

1
2
word = "Coding"
print(len(word))  # 6

That length means Python knows exactly where each character lives, and the total length of characters.


Unpacking a String

Unpacking a string lets you split a sequence of characters - from a string or list - into individual variables. This is a great way to see Python’s sequencing feature in action.

Each character or item is stored at an index starting from 0, and unpacking simply assigns each one to its own variable.

When I first saw this, it felt like a neat trick. But looking back, the real value comes from thinking about how you could apply it in different situations—like automation or processing data efficiently.

1
2
3
4
5
6
7
8
9
word = "Coding"
a, b, c, d, e, f = word

print(a)  # C
print(b)  # o
print(c)  # d
print(d)  # i
print(e)  # n
print(f)  # g

Indexing (Accessing Individual Characters or Items)

Indexing is a term used to refer to two concepts:

1) Index referencing and, 2) Accessing items/characters

To reiterate, indexing is the use of storing characters or items in your device starting from index position 0. You can use this information to bring back that data from storage:

1
2
3
word = "coding"
print(word[0])  # c
print(word[-1]) # g

You can access items either using positive or negative numbers.

Positive numbers will start from 0 and work to the last item in the variable, negative numbers will start at -1 then work towards the beginning of the variable.


Slicing (Taking Pieces of a String)

Slicing is all about setting 3 parameters of a variable, which enable you to specify a start, stop, and skip values.

These parameters are the used to reproduce the string in that format, without changing the initial variable.

This is important to know because if a function changes the intial value it means it is not manipulating data to present new data, but tranforming it.

1
2
3
4
5
6
7
name = "sheikh"
print(name[:3])   # she
print(name[3:])   # ikh
print(name[::-1]) # hkiehs (prints everything in reverse)

print(name[0:3])   # she
print(name[3:6])   # ikh

[start : stop : step/skip]

Using this notation, you can select a portion of a sequence (like a string or list).

  • Start is the index where you begin

  • Stop is the index just before where you end - think minus 1

  • Step/Skip tells Python how many items to skip each time and select the index it lands on

Tip: If you leave start or stop empty, Python will automatically use the beginning or end of the sequence.


String Methods

This is where we begin to get a feel of how vast Pythons library is when it comes to built-in functions.

String methods are built-in functions designed to be used with strings only.

On Pythons Official Documentation for string methods, you’ll find the complete list of methods/functions available.

Think of these as:

Small tools that answer questions, boolean, or manipulate text.


Final thoughts:

Overall, relearning about string data type and the functions you are able to perform with them has helped me learn that:

  • Strings are structured, not random text
  • Indexing and slicing are foundational concepts
  • Formatting is essential for real programs
  • String methods are helpers, not hurdles

Being able to go over the topics again and spending a whole day rewriting my notes on them provide me with the key insights I need to understand how coding wiht Python works.

If you’ve found this interesting, have some thoughts, questions, or improvements I could make - let me know and I’d love to connect.

Footnotes

This post is licensed under CC BY 4.0 by the author.