String Operations
Table of Contents + −
A name. An email. A message someone typed. All of these are strings. A string is just text. It is a row of characters sitting one after another. But raw text on its own does not do much. You have to measure it. Cut it. Join it. Check it. That is what string operations are. They are the small jobs you do on text again and again in real programs.
Let me walk you through the common ones. We will do each one in a tiny program. That way you see the input and the exact output side by side.
🎯 The Pain First
Say you are building the sign-up screen for an app. A user types their full name as “Riya Sharma”. Now your code has to do real work on that text.
- You want to check the name is not too short. So you need its length.
- You want to grab just the first letter for a profile badge. So you need one character by its index.
- You want to greet them with “Hello, Riya Sharma!”. So you need to join pieces of text together.
Every one of these is a string operation. Learn the handful below. After that, most everyday text work feels easy.
📏 Length: How Long Is the Text?
The length of a string is the count of characters in it. So “Riya” has a length of 4.
You ask for length when you want to check input or loop over each character. Is a password too short? Is a tweet too long? Both checks start with length.
🔤 Access a Character by Index
A string is ordered. Each character sits at a position called an index. And indexing starts at 0. So in “Riya” the character at index 0 is ‘R’. The character at index 1 is ‘i’.
You use this when you want one specific letter. The first letter for a badge. The last letter to check something. Just remember the start is index 0, not 1.
🔗 Concatenate: Join Two Strings
Concatenation means sticking strings together to make one longer string. So joining “Hello, ” and “Riya” gives you “Hello, Riya”.
This is how you build messages out of parts. A greeting. A file path. A full name made from a first name and a last name.
Joining text again and again can get slow
In many languages a string cannot be changed after it is made. So each join builds a brand new string and copies the old characters over. Do that inside a big loop and the cost adds up. We will look at this in the complexity note below.
✂️ Substring: Take a Slice
A substring is a smaller piece taken out of a string. You pick a start position and an end position. You get back the characters in between. So from “Riya Sharma”, a slice from index 0 up to 4 gives you “Riya”.
You reach for this when you want only part of the text. The area code from a phone number. The file name from a path. The first word from a sentence.
⚖️ Compare Two Strings
Comparing strings checks if two pieces of text are the same. Or which one comes first in dictionary order. So “apple” and “apple” are equal. And “apple” comes before “banana”.
You compare strings all the time. Checking a typed password against the saved one. Sorting names into A to Z order. Matching a search word.
Use the right tool to compare
In C you compare strings with strcmp, not with ==. In Java use .equals, not ==. The == there checks if it is the same object in memory, not if the text matches. Python and JavaScript let you use == on the text directly.
🔡 Convert Case
Changing case means turning every letter to uppercase or to lowercase. So “Riya” in lowercase is “riya”. In uppercase it is “RIYA”.
This is mostly used to compare text fairly. A user might type “GMAIL” or “gmail”. If you lowercase both first, they match. So case conversion makes your checks forgiving.
🛠️ One Program, Many Operations
Now let me put all of these into one small program. We start with the string “Riya Sharma”. Then we run each operation on it and print the result. Read the comments and the output together.
Program for common string operations in Python
# Run common string operations and print each resultdef string_ops(text): # 1) Length: len() returns the character count print("Length:", len(text))
# 2) Character at index 0 (the first character) print("Char at index 0:", text[0])
# 3) Concatenate: + joins two strings into one print("Concatenated:", "Hello, " + text)
# 4) Substring: slicing text[start:end] takes a slice print("Substring (0..4):", text[0:4])
# 5) Compare (True means the text is equal) print("Equals 'Riya Sharma':", text == "Riya Sharma")
# 6) Convert to uppercase print("Uppercase:", text.upper())
def main(): text = "Riya Sharma" string_ops(text)
if __name__ == "__main__": main()The output of the above code will be:
Length: 11Char at index 0: RConcatenated: Hello, Riya SharmaSubstring (0..4): RiyaEquals 'Riya Sharma': TrueUppercase: RIYA SHARMANotice the length is 11, not 10
The space between “Riya” and “Sharma” counts as a character too. So “Riya Sharma” has length 11. A space is a real character. Never forget it when you measure text.
⏱️ A Quick Word on Cost
Most of these operations are cheap. But not all of them. And it helps to know which is which.
- In Python, JavaScript, Java, and C++, the length is stored with the string. So asking for length is O(1). It is instant, no matter how long the text is.
- In C there is no stored length.
strlenwalks the whole string and counts characters until it hits the end marker. So in C length is O(n), where n is the number of characters. - Reading one character by index is O(1) everywhere. You jump straight to that position.
- Concatenation, substring, and case conversion all build a new string for the result. So each one is O(n). It touches every character it copies.
That concatenation cost is the one that catches people out. Joining text once is fine. But joining inside a long loop copies more and more each time. So the total work grows fast.
Build big text the smart way
Are you joining many pieces in a loop? Then do not keep using + on a string each round. Collect the pieces in a list and join them once at the end. Python has "".join(list). Java has StringBuilder. The cost drops a lot.
📊 Complexity of Common String Operations
Here n is the number of characters in the string.
| Operation | Time Complexity | Note |
|---|---|---|
| Length | O(1) usually, O(n) in C | Stored in most languages; C counts every character |
| Access by index | O(1) | Jump straight to the position |
| Concatenate | O(n) | Builds a new string and copies characters |
| Substring | O(k) | k is the length of the slice taken |
| Compare | O(n) | Stops early at the first character that differs |
| Convert case | O(n) | Touches every character once |
⚠️ Common Mistakes
A few small slips trip up almost every beginner. Let me point them out.
- Thinking index starts at 1. The first character is at index 0, not 1. So the last character of a string of length n sits at index n minus 1.
- Forgetting spaces count. A space is a character. So “Riya Sharma” is length 11. And trailing spaces sneak into your length and your compares.
- Using
==to compare text in Java or C. Use.equalsin Java andstrcmpin C. The==there checks the wrong thing. - Joining strings inside a big loop. Each
+makes a fresh copy. Over many rounds that gets slow. Collect the pieces and join them once. - Mixing up case when comparing. “Gmail” and “gmail” are not equal as plain text. Lowercase both first if you want a fair match.
🧩 What You’ve Learned
You now know the everyday string operations. And you know what each one costs. Here is the recap.
- ✅ Length gives the character count, and spaces count too.
- ✅ Characters sit at indexes starting from 0, and reading one is instant.
- ✅ Concatenation joins strings, and substring takes a slice out of one.
- ✅ Compare checks if text is equal or which comes first, and the right tool differs by language.
- ✅ Case conversion turns text all upper or all lower, mostly to make compares fair.
- ✅ Length is O(1) in most languages but O(n) in C, while concatenate, substring, and case conversion all cost O(n).
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is the length of the string "Riya Sharma"?
Why: There are 10 letters plus the one space between the words, so the length is 11.
- 2
In the string "Riya", which character is at index 0?
Why: Indexing starts at 0, so index 0 is the first character, which is 'R'.
- 3
Why can joining strings inside a long loop become slow?
Why: In most languages each concatenation creates a new string and copies the existing characters, so the work piles up across many loops.
- 4
Which statement about the length operation is correct?
Why: Most languages store the length so it is O(1), but C has no stored length and strlen walks the whole string, making it O(n).
🚀 What’s Next?
You can measure, cut, join, and compare text now. Build on that next.
- Introduction to Strings goes deeper into what a string really is and how it sits in memory.
- String Searching shows how to find one piece of text inside another, which is where these basics start paying off.
Practice each operation once on your own name. After that, the rest of string work will feel natural.