What is String ?
- String is an datatype in python. It is represented as 'str' in python.
- String is enclosed with single(' ') or double quotes("") , if we want to print multiline string we can use triple quotes(""" """).
- String is collection of characters ,characters can be anything , includes alphabets ,special characters and numbers.
- String is immutability.(nce created, a string cannot be modified in place; methods like .upper() or .replace() return a new string object).
- In python there positive and negative indexing , positive index start from zero (lest to right) and negative index start from -1 (right to letft).
text = "H e l l o"
0 1 2 3 4 ---->(positive indexing)
-5-4-3-2-1 ---->(Negative indexing)
why string is required ?
to store, display, and manipulate text—such as words, sentences, symbols, and numbers treated as characters rather than math values.
Common Uses of Strings
- Displaying text: Showing labels, menus, error messages, and UI content on screens.
- User input and output: Reading form fields, command-line text, and search queries.
- Data exchange: Sending and receiving structured formats like JSON, XML, CSV, or URLs.
- Identifiers and paths: Storing usernames, IDs, file paths, and dates before parsing or formatting.
When Not to Use Strings ?
- Math calculations: Use numbers (int, float, Decimal) instead of numeric strings so math works accurately.
- True/false logic: Use booleans (true/false) rather than strings like "yes" or "true".
- Fixed choices: Use enums or constants when values come from a strict set.
String Function
1) Capitalise()---> methods returns a string were first character in upper case.
text = "hello"
print(text.capitalise())
#output
#Hello
2) LowerCase()--> methods return a string were all character in lower Case.
text = "HEllo"
print(text.lower())
# output
#hello
** 3) UpperCase()**--> methods return a string were all character in upperCase.
text = "hello"
print(text.upper())
#output
#HELLO
4) Split()---> method breaks a string into a list of smaller substrings based on a specified separator (delimiter).Delimiter can be space , char , special char etc,
- The output in list format.
name = 'To is monday'
print(name.split( )) # split by space
#output
['To', 'is', 'monday']
5) Replace()---> Method change the old value into new value.
text = "I love java , java"
print(text.replace('java','Python'))
6) joins---> method combines all items from an iterable (such as a list, tuple, or set) into a single string using a specified separator.
words = ["Python", "is", "easy"]
print("-".join(words))
why String is immutable?
Strings in Python are immutable—meaning their contents cannot be changed after creation—primarily to guarantee hashability, memory efficiency, and thread safety.
If we try to update the existing string context , the existing string cannot get changes , instead a new memory will be crated for the edited /updated content will be add.
Example:
name = 'python'
print(id(name)) #132887615224592
name = 'Python'
print(id(name)) #11043008
Here , variable name is same but value is different so different memory created for each object(Here every datatype is object).
we updating variable it will disconutied and create new memory space.
name_1 = 'python'
print(id(name_1)) #127644993190672
name_2 = 'python'
print(id(name_2)) #127644993190672
name_3 = 'python'
print(id(name_3)) #127644993190672
Here the same memory for different user so the memory efficiency is high and memory required is low.
Top comments (0)