Table of Contents:
Computer Programming Strings – Definition, Examples & String Operations
A string is a sequence of characters grouped together to represent text. Strings are used to store names, messages, words, sentences, and any form of textual data.
Example:
"Hello"
"Welcome to Programming"
Strings are one of the most widely used data types in computer programming.
What Is a String?
A string is a collection of characters enclosed within quotes.
- In C → strings use double quotes
- In Java → strings are objects of the
Stringclass - In Python → strings are immutable sequences of Unicode characters
Example Values:
"John"
"12345"
"Hello World!"
Strings in Different Programming Languages
programming strings, what is a string, C strings, Java string class, Python strings, string operations, string concatenation, substring, string manipulation, immutable strings
Strings in C
In C, strings are stored as arrays of characters, ending with a special character ‘\0’ (null terminator).
Declaration
char name[10];
Initialization
char name[] = "John";
Accessing Characters
printf("%c", name[0]); // Output: J
Strings in Java
Java treats strings as objects of the built-in String class.
Declaration & Initialization
String name = "John";
Accessing Characters
System.out.println(name.charAt(0)); // Output: J
Useful Methods
name.length();
name.toUpperCase();
name.toLowerCase();
name.contains("oh");
Strings in Python
Python strings are immutable and very flexible.
Declaration
name = "John"
Accessing Characters
print(name[0]) # Output: J
Useful Methods
name.upper()
name.lower()
name.replace("J", "K")
len(name)
String Operations
1. Concatenation
Join two or more strings together.
C Example
strcat(str1, str2);
Java Example
String full = str1 + str2;
Python Example
full = str1 + str2
2. Length of a String
C
strlen(name);
Java
name.length();
Python
len(name)
3. Substrings
Extract part of a string.
Java
name.substring(1, 3);
Python
name[1:3]
4. Comparing Strings
Java
name.equals("John");
Python
name == "John"
Immutable vs Mutable Strings
Immutable Strings
Once created, cannot be changed.
- Java
String - Python
str
Mutable Strings
Can be modified.
- C character arrays
- Java
StringBuilder/StringBuffer
String Applications
Strings play a vital role in:
- User input handling
- Data processing
- Text parsing
- File operations
- Web development
- Communication between systems
- Database queries
FAQ
What is a string in programming?
A string is a sequence of characters used to represent text.
Are strings mutable?
In C, character arrays are mutable.
In Java and Python, strings are immutable.
How do I find the length of a string?
Use strlen() in C, .length() in Java, and len() in Python.
How do I combine two strings?
You can concatenate using + in Java and Python.
In C, use strcat().
Can strings contain numbers?
Yes, as long as they are enclosed in quotes.