Here's a comprehensive explanation of the string data type in Python:
What are strings?
Sequences of characters used to represent textual data.
Enclosed within single quotes (''), double quotes (""), or triple quotes (''' or """).
Used for names, messages, descriptions, code comments, etc.
Key characteristics:
Immutable: Cannot be modified in place. You create new strings with changes.
Ordered: Characters have a specific order, accessible by index (starting from 0).
Iterable: Can be looped through using for loops.
Common operations:
Concatenation: Joining strings:
Python
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name # Output: Alice SmithSlicing: Extracting substrings:
Python
greeting = "Hello, world!"
first_five = greeting[0:5] # Output: HelloIndexing: Accessing individual characters:
Python
message = "Python is awesome!"
first_letter = message[0] # Output: PFormatting: Inserting variables into strings:
Python
name = "Bard"
age = 30
introduction = f"Hello, my name is {name} and I'm {age} years old."Finding: Searching for substrings:
Python
text = "This is a sample text."
is_found = "sample" in text # TrueReplacing: Substituting parts of a string:
Python
sentence = "I love coding in Python."
new_sentence = sentence.replace("Python", "Java") # I love coding in Java.Splitting: Dividing a string into a list of substrings:
Python
words = "apple, banana, cherry".split(",") # Output: ["apple", "banana", "cherry"]Upper/lower: Converting case:
Python
title = "the lord of the rings".title() # Output: The Lord Of The RingsChecking: Determining string properties:
Python
is_empty = "".isspace() # True
starts_with_python = "Python is cool".startswith("Python") # True
Additional notes:
Python has extensive string methods for various manipulations and transformations.
Use string formatting techniques (f-strings, format() method, etc.) for flexible output.
Consider using string libraries like string for advanced operations.
a=20
b=30
c=a+b
c
50
print(c)
50
name='venkat'
name
'venkat'
name="venkat"
name
'venkat'
print(name)
venkat
cost =30
print( cost + 'in Rupees')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[10], line 1 ----> 1 print( cost + 'in Rupees') TypeError: unsupported operand type(s) for +: 'int' and 'str'
cost=30
value="in RS"
print(cost + value)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[11], line 3 1 cost=30 2 value="in RS" ----> 3 print(cost + value) TypeError: unsupported operand type(s) for +: 'int' and 'str'
print(str(cost)+' '+value)
30 in RS
name= 'ram's'
Cell In[15], line 1 name= 'ram's' ^ SyntaxError: unterminated string literal (detected at line 1)
name= "ram's"
name
"ram's"
print(name)
ram's
name= 'Vlr training'
print(name[0])
V
print(name[3])
print(name[13])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[21], line 1 ----> 1 print(name[13]) IndexError: string index out of range
sonname='venkat'
FathName='Seshu'
print(sonname + FathName)
venkatSeshu
print(sonname +" "+FathName)
venkat Seshu
print(sonname +" Father Name is "+FathName)
venkat Father Name is Seshu
print("ram\"s")
ram"s
print('ram\'s')
ram's