Fundamental of Python : 2.Python Strings
Fundamentals of Python
2. Python Strings
Python also allows dynamic typing, so the type is automatically inferred at runtime when values are assigned.
Key operations include:
- Concatenation: Combining multiple strings.
- Length Calculation: Determining the number of characters.
- Type Conversion: Converting strings to integers and vice versa.
- Formatting: Embedding variables or values within strings.
- Case Manipulation: Converting to upper or lower case.
- Substring Access: Extracting portions of the string using indexing and slicing.
- Trimming: Removing specified leading and trailing characters using string methods.
Let’s explore string operations with examples:
🔹 1. String Creation
Program:'hello' is the same as "hello".You can display a string literal with the print() function:#Create string>>>s="Hello World!">>>type(s)Result : <type ’str’>
Explanation: The variable s is assigned a string. Python recognizes it as a string type.
🔹 2. String Concatenation
Program:#String concatenation
>>>t="This is sample program."
>>>r = s+t
>>>rResult : 'Hello World!This is sample program.
Explanation: Using the + operator joins two strings. No automatic space is inserted between them.
🔹 3. Length of a String
Program:#Get length of string
>>>len(s)Result : 12
Explanation: len() returns the number of characters, including spaces and punctuation.
🔹 4. String to Integer Conversion
Program:#Convert string to integer
>>>x="100"
>>>type(s)
result : <type ’str’>
>>>y=int(x)
>>>y
Result : 100
Explanation: The string "100" is successfully converted to an integer using int().
🔹 5. Printing a String
Program:#Print string>>>print sResult : Hello World!
Explanation: The print() function displays the string to the output screen.
🔹 6. Formatting Output
Program:#Formatting outputResult : "The string (The string (Hello World!) has 12 characters)
Explanation: The format() method dynamically inserts values into a string template.
🔹 7. Case Conversion
Program:#Convert to upper/lower case
>>>s.upper()
Result : 'HELLO WORLD!’
>>>s.lower()
Result : 'hello world!’
Explanation: upper() converts all characters to uppercase; lower() converts them to lowercase.
🔹 8. Accessing Substrings
Program:#Accessing sub-strings
>>>s[0]
Result : 'H’
>>>s[6:]
Result : 'World!’
>>>s[6:-1]
Result : 'World’
Explanation:
s[0] accesses the first character.
s[6:] extracts from the 7th character to the end.
s[6:-1] retrieves a slice excluding the last character.
🔹 9. Trimming Characters
Program:
#strip: Returns a copy of the string with the #leading and trailing characters removed.
>>>s.strip("!")
Result : 'Hello World’
Explanation: The strip() method removes the specified characters from both ends of the string—in this case, the exclamation mark.
🔸 Summary:
Python strings are powerful tools for managing and processing text. From accessing specific characters to formatting and transforming data, strings offer a wide range of functionalities crucial in every Python application.
📢 If you found this post helpful, don’t forget to share it with your fellow learners, follow for more Python tips, and drop a comment below with your thoughts or questions! 🐍💬

Comments
Post a Comment