Knots and Knives – Untangling Strings and States in Python meaning

Anindita GhoshAnindita Ghosh
6 min read

Python Data Types-

Python has built-in data types used to store different kinds of data. It is dynamically typed, which means we don’t need to declare the data type of a variable, Python automatically identifies it when a value is assigned. In python there are many types of data - like ‘Integer’ , ‘Float’ , ‘String’ ,’ Boolean’ , ‘List’ and many more . But in this blog will discuss about String data type.

So let’s get started.

Let’s Talk About Strings in Python

In Python, a string is one of the most commonly used data types — and also one of the most important.
A string is defined as a sequence of characters — which can include letters, digits, punctuation marks, whitespace — enclosed within single (') or double (") quotes. Whether it’s displaying a welcome message, storing a user's name, or processing input from a file — all are string data type. Strings are used extensively in Python for data storage, manipulation, and output formatting.

"Hello, Python!"
"Welcome in Chiacode"
'12345'
"Email: example@domain.com"

String methods:

In Python, a method is like a special tool that belongs to a specific type of object— it helps to do something with that object. For example, if we want to turn a string into all uppercase letters, we can use the .upper() method. If we want to remove extra spaces from the beginning or end of a string, we can use .strip(). There are many methods in string; let’s explore them. Here comes a important question - “how to know all the methods we can apply in a string methods?” Here is a representation how we can or there has also another way by using “help()” method you can know all the methods we can apply in a string .

Alt Text

》Some examples of string methods:

text = "hello"
# upper() – Converts string to uppercase
print(text.upper())  # Output: HELLO

text = "HELLO"
# lower() – Converts string to lowercase
print(text.lower())  # Output: hello

text = "python is fun"
# capitalize() – Capitalizes first letter
print(text.capitalize())  # Output: Python is fun

# title() – Capitalizes the first letter of each word
print(text.title())  # Output: Python Is Fun

text = "one,two,three"
# split() – Splits string into a list
print(text.split(","))  # Output: ['one', 'two', 'three']

text = "hello world"
# find() – Returns index of first match
print(text.find("world"))  # Output: 6

text = "  hello  "
# strip() – Removes leading/trailing whitespace
print(text.strip())  # Output: 'hello'

length = len(text)
# 
print(length)  # Output: 5

Though there are many methods for ‘string’. Now we will discuss about length of a string and Indexing methods of string.

word = "STRING"
print(len(word))  # output -> 5

》Picking a Single Character by Indexing method:

word = "STRING"
word[0]     # Output: 'S' (First character) [use +ve indexing]
word[-1]    # Output: 'G' (last character)  [use -ve indexing]

》Here a representation of Picking Single Character by Indexing method:

A Detailed Explanation of Alan Becker's “Animation vs. Coding” | by Shuai  Li | Mar, 2025 | Medium

Now suppose we want to grab only few characters from a string then what to do ?

Here comes the method ‘slicing’. Slicing just means we’re grabbing a piece (a slice) of that line/that string

Here are Example Code Showing Different Types of Slicing

text = "Python"
numbers = [10, 20, 30, 40, 50, 60]

# 1. Basic slicing (start:stop)
print(text[0:3])        # 'Pyt'
print(numbers[1:4])     # [20, 30, 40]

# 2. Slice from start to specific index
print(text[:4])         # 'Pyth'
print(numbers[:3])      # [10, 20, 30]

# 3. Slice from specific index to end
print(text[2:])         # 'thon'
print(numbers[3:])      # [40, 50, 60]

# 4. Full slice (copy whole thing)
print(text[:])          # 'Python'
print(numbers[:])       # [10, 20, 30, 40, 50, 60]

# 5. Slicing with step
print(text[::2])        # 'Pto' (every 2nd letter)
print(numbers[::3])     # [10, 40]

# 6. Reverse slicing
print(text[::-1])       # 'nohtyP'
print(numbers[::-1])    # [60, 50, 40, 30, 20, 10]

# 7. Slicing with negative start/stop
print(text[-4:-1])      # 'tho'
print(numbers[-5:-2])   # [20, 30, 40]

# 8. Reverse with step
print(text[-1:-6:-2])   # 'nhy'

Now we will discuss about f-string’. What is ‘f-string’ and why we use !!!

name = "Anindita"
print(name + 4)

#Output -> (TypeError: can only concatenate str (not "int") to str)

The above code will give us TypeError , to solve this issue here comes f-string’ and take a major role.

name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")

# Output: My name is Alice and I am 25 years old.

We got the concept of f-string’ but what happend if we multiplly a numer with a string let’s check :

# Example of string repetition
print("hello" * 2) 
# output -> hellohello

print("hi" * 3)
# output -> hihihi

What's an ASCII value?

Comparison operator(↖︎click here to know abot operator) do compare between two value . But for string here are some rules of comparing between two strings. Here we came to know about ASCII VALUE(↖︎) , in this table we came to know that Every character we type—like 'A', 'b', '3', '!', or even a space—has a specific number behind it that computers actually use.
That secret number is called the ASCII value. Here is the ASCII TABLE —

Lightbox

str1 = "python"
str2 = "javascript"

print(str1 == str2)    # output -> False
print(str1 != str2)    # output -> True
print(str1 > str2)     # output -> True
print(str1 < str2)     # output -> False
print(str1 >= str2)    # output -> True
print(str1 <= str2)    # output -> False

# Some examples that demonstrate ord() values and string comparisons in Python: 

ord('b')             # 98
ord('A')             # 65
"Dog" < "dog"        # True     # because 'D' < 'd' (68 < 100)
"hello" > "Hello"    # True     # 'h' > 'H' (104 > 72)

Conclusion:

Strings in Python are a fundamental data type used to store sequences of characters, such as words, sentences, or symbols. From simple operations like indexing, slicing, and concatenation to more advanced tasks like searching, replacing, formatting, and checking conditions—strings are powerful and flexible and also string is an immutable object , thread-safe and prevent accidental changes, making code more predictable (Will discuss about mutable and Immutable in another blog). Python also provides many built-in methods to manipulate strings—like changing case, finding substrings, replacing text, and splitting or joining words. Because strings are so commonly used in programs understanding how to work with them efficiently is a necessery skill for Python programming.

Here is my previous blog about operator.

Thanks for reading!😁

40
Subscribe to my newsletter

Read articles from Anindita Ghosh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anindita Ghosh
Anindita Ghosh