String
String
A string is a sequence of characters used to represent text. It's one of the most
commonly used data types in almost every programming language.
🔹 String Characteristics:
Can include letters, numbers, symbols, whitespace
Enclosed in quotes (single ' ', double " ", or backticks `, depending on the
language)
✅ Examples of Strings:
text
Copy
Edit
"Hello World"
'12345'
"Welcome to India!"
'@#&$%'
" "
"John_Doe_99"
"true" ← (Note: This is a string, **not** a boolean)
🔸 Strings in Popular Programming Languages
🔹 Python
python
Copy
Edit
s1 = "Hello"
s2 = 'World'
s3 = "123"
s4 = "Hello " + "World" # Concatenation
print(s4.upper()) # HELLO WORLD
🔹 Java
java
Copy
Edit
String s1 = "Hello";
String s2 = "World";
System.out.println(s1 + " " + s2); // Hello World
System.out.println(s1.length()); // 5
🔹 JavaScript
javascript
Copy
Edit
let str1 = "Hello";
let str2 = 'World';
let str3 = `Name: ${str1}`; // Template literal
console.log(str1 + " " + str2); // Hello World
🔹 C
c
Copy
Edit
char str[] = "Hello";
printf("%s", str); // Output: Hello
🔹 Common String Operations:
Operation Example Description
Length "hello".length Returns number of characters
Concatenation "hello" + " world" Joins two strings
Upper/Lower "Hello".toUpperCase() HELLO
Substring "Hello".substring(1, 3) "el"
Replace "abc".replace("a", "x") "xbc"
Trim " hi ".trim() "hi"
Split "a,b,c".split(",") ["a", "b", "c"]
IndexOf "hello".indexOf("e") 1
CharAt "hello".charAt(1) "e"
Example:
python
Copy
Edit
print("Hello\nWorld")
# Output:
# Hello
# World
🔹 String vs Other Types
Type Example Description
String "123" Text
Integer 123 Number (no quotes)
Boolean true Logical (true/false)
Character 'A' Single character (Java, C)