String Datatype In Python

Explore and Read Our Blogs Written By Our Insutry Experts Learn From KSR Data Vizon

The string is a sequence of characters enclosed in a single (‘ ‘)  or double (” “)  or triple quotes (”’ ”’ or “””  “””).

  • Immutable: Once created, strings cannot be modified. Any operation that alters a string will return a new string.
  • Indexed: Each character in a string has a unique index (starting at 0 for the first character).
  • Iterable: Strings are iterable, meaning you can loop through each character using a for loop.

Creating Strings

# Using single, double, or triple quotes
str1 = 'Hello'
str2 = "World"
str3 = '''Triple quotes are useful for
multi-line strings'''

# Creating Empty string
empty_str_1 = ' '
empty_str_2 = str()

print(str1)
print(str2)
print(str3)

print(empty_str_1)
print(empty_str_2)

Output:

Hello
World
Triple quotes are useful for multi-line strings
' '
' '

String Indexing and Slicing

course = "Python"

# Indexing
print(course[0])
print(course[-1])

# Slicing
print(course[1:4])
print(course[:3])
print(course[2:])
print(course[::-1])

Output:

P      # first character
n      # last character
yth    # from index 1 to 3
Pyt    # from start to index 2
thon   # from index 2 to the end
nohtyP # reversed string

Example: Reverse a String:

s = "advanced"
reversed_s = s[::-1]
print(reversed_s)

Output:

decnavda

Mathematical Operators for String

In Python, you can use certain operators with strings:

  1. Concatenation (+): Combines two strings into one.
  2. Repetition (*): Duplicates the string a given number of times.

Example:

print("KSR" + " DATAVIZON")
print("KSR" * 3)

Output:

KSR DATAVIZON
KSRKSRKSR

Important points:

  1. The + operator can only concatenate if both values are strings.
  2. The * operator works when one value is a string and the other is an integer.

Common Functions for Strings

FunctionDefinition
len()Returns the number of characters in a string.
min()Returns the smallest character in a string based on Unicode.
max()Returns the largest character in a string based on Unicode.
reversed()Returns an iterator that accesses the string in reverse order.
sorted()Returns a sorted list of the characters in a string.

Example:

hw = "Hello, World!"

# 1. Length of the string
length = len(hw)  # Returns the number of characters
print(f"Length: {length}")  

# 2. Minimum character
smallest_char = min(hw)  # Returns the smallest character based on Unicode
print(f"Smallest character: '{smallest_char}'")  

# 3. Maximum character
largest_char = max(hw)  # Returns the largest character based on Unicode
print(f"Largest character: '{largest_char}'")  

# 4. Reversed string
reversed_string = ''.join(reversed(hw))  # Reverses the string
print(f"Reversed string: '{reversed_string}'")  

# 5. Sorted characters
sorted_chars = sorted(hw)  # Returns a sorted list of characters
print(f"Sorted characters: {sorted_chars}") 

Output:

Length: 13
Smallest character: ' '
Largest character: 'r'
Reversed string: '!dlroW ,olleH'
Sorted characters: [' ', '!', ',', 'H', 'W', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r']

1. Changing Case:

  • str.upper(): Converts all letters in the string to uppercase.
  • str.lower(): Converts all letters in the string to lowercase.
  • str.capitalize(): Capitalizes the first letter of the string.
  • str.title(): Capitalizes the first letter of each word in the string.
s = "hello world"
upper_s = s.upper()         
lower_s = s.lower()         
capitalized_s = s.capitalize()  
title_s = s.title()

print(upper_s)
print(lower_s)
print(capitalized_s)
print(title_s) 	

Output:

HELLO WORLD
hello world
Hello world
Hello World

2. Stripping Whitespace:

  • str.strip(): Removes whitespace from both the beginning and end of the string.
  • str.rstrip(): Removes whitespace from the end of the string only.
  • str.lstrip(): Removes whitespace from the beginning of the string only
hw = "  Hello, World!  "
print(hw.strip())
print(hw.lstrip())
print(hw.rstrip())

Output:

Hello, World!
Hello, World!  
  Hello, World!

3. Replacing Substrings:

  • str.replace(old, new): Replaces all occurrences of the specified old substring with a new substring.
phrase = "I love Python"
new_phrase = phrase.replace("love", "enjoy")
print(new_phrase)

Output:

I enjoy Python

4. Splitting and Joining Strings:

  • str.split(delim): Splits the string into a list using the specified delimiter.
  • str.join(iterable): Joins elements of an iterable object (like a list) into a single string, using the string as a separator.
sentence = "Python is fun"
words = sentence.split()  # Split by whitespace (default)
print(words)

words_2 = sentence.split('i') # Split by i
print(words_2)

joined = " ".join(words)  # Join the list back into a string
print(joined)

Output:

['Python', 'is', 'fun']
['Python ', 's fun']
Python is fun

5. Finding Substrings

  • find(substring): Returns the index of the first occurrence of the substring. Returns -1 if not found.
  • rfind(substring): Returns the index of the last occurrence of the substring. Returns -1 if not found.
  • find(substring, begin_index, end_index): Searches for the substring within the specified range. Returns -1 if not found.
  • index(substring): Similar to find(), but raises a ValueError if the substring is not found.
  • rindex(substring): Similar to rfind(), but raises a ValueError if the substring is not found.
s = "Exploring data with Python is fun"
print(s.find("data"))    
print(s.find("SQL"))     
print(s.find("i"))       
print(s.rfind("i"))      

# Additional examples with a range: 
print(s.find('a', 7, 15)) 
print(s.find('z', 7, 15))

s2 = "Machine learning with Python is powerful"
print(s2.index("learning"))
print(s2.index("Python"))
print(s2.index("i"))
print(s2.rindex("i"))

# Uncommenting the following line will raise a ValueError because "KSR" is not in the string
# print(s.index("KSR"))     # ValueError: substring not found

Output:

10  # First occurrence of "data"
-1  # Since "SQL" is not found
6   # First occurrence of "i"
27  # Last occurrence of "i"
11  # First occurrence of 'a' between index 7 and 15
-1  # Since 'z' is not found in the range
8   # First occurrence of "learning"
22  # First occurrence of "Python"
4   # First occurrence of "i"
29  # Last occurrence of "i"

6. Checking if String is Alphabetic or Numeric

  • str.isdigit(): Returns True if all characters in the string are digits; otherwise, returns False.
  • str.isalpha(): Returns True if all characters in the string are alphabetic; otherwise, returns False.
  • str.isalnum(): Returns True if all characters in the string are alphanumeric (contains both letters and numbers); otherwise, returns False
print("Python".isalpha())
print("12345".isdigit())
print("Python123".isalnum())

Output:

True # all characters are alphabetic
True # all characters are digits
True # all characters are alphanumeric

7. Counting substring in the given String:

  • str.count(substring): Returns the number of non-overlapping occurrences of the specified substring in the string
s = "hellohellohelloworld"
print(s.count('o'))
print(s.count('hello'))
print(s.count('l', 5, 15))

Output:

4 # Counts occurrences of 'o'
3 # Counts occurrences of 'hello'
4 # Counts occurrences of 'l' between index 5 & 15

8. Alignment Methods for string

  • str.center(width) : This method returns a string that is centered within a given width, padded with spaces by default.
  • str.ljust(width): This method makes a string left-aligned in a given width, adding spaces to the right.
  • str.rjust(width): This method makes a string left-aligned in a given width, adding spaces to the left.
text = "Hello"

# Center the text
centered = text.center(10)
print(f"Centered: '{centered}'")

# Left-justify the text
left_justified = text.ljust(10)
print(f"Left-justified: '{left_justified}'")

# Right-justify the text
right_justified = text.rjust(10)
print(f"Right-justified: '{right_justified}'")

Output:

Centered: '  Hello   '
Left-justified: 'Hello     '
Right-justified: '     Hello'

9. Checking starting and ending part of the string:

  • s.startswith(substring): Returns True if the string starts with the specified substring; otherwise, returns False.
  • s.endswith(substring): Returns True if the string ends with the specified substring; otherwise, returns False.
s = "Hello, world!"
print(s.startswith("Hello"))
print(s.startswith("world"))

print(s.endswith("world!"))
print(s.endswith("Hello"))

Output:

True
False
True
False

10. Check type of characters present in a string:

  • str.islower(): Returns True if all characters in the string are lowercase letters; otherwise, returns False.
  • str.isupper(): Returns True if all characters in the string are uppercase letters; otherwise, returns False.
  • str.istitle(): Returns True if the string is in title case (first letter of each word is uppercase); otherwise, returns False.
  • str.isspace(): Returns True if the string contains only whitespace characters; otherwise, returns False.
s1 = "hello"
s2 = "WORLD"
s3 = "Hello World"
s4 = "   "
s5 = "  a  "

# Using islower()
print(s1.islower())
print(s2.islower())

# Using isupper()
print(s1.isupper())
print(s2.isupper())

# Using istitle()
print(s3.istitle())
print(s1.istitle())

# Using isspace()
print(s4.isspace())
print(s5.isspace())

Output:

True
False
False
True
True
False
True
False

String Formatting

In Python, there are multiple ways to format strings with variable values. Three commonly used methods include the % operator, the str.format() method, and f-strings (introduced in Python 3.6).

1. Using the % Operator

he % operator is used for formatting strings with placeholders like %s for strings, %d for integers, and %f for floats.

name = 'Alice'
age = 30
height = 5.6

formatted_string = "%s is %d years old and %.1f feet tall." % (name, age, height)
print(formatted_string)

Output:

Alice is 30 years old and 5.6 feet tall.

Placeholders:

  • %s for strings
  • %d for integers
  • %f for floating-point numbers (use %.nf for n decimal places)

2. Using the format() Method

The str.format() method offers greater flexibility and readability in string formatting. You can use curly braces {} as placeholders and call the format() method with the values you want to insert.

name = 'Alice'
product = 'Smartphone'
price = 499.99
quantity = 3

formatted_string = "{} costs ${:.2f} and there are {} available.".format(product, price, quantity)
print(formatted_string)

Output:

Smartphone costs $499.99 and there are 3 available.

Key Features:

  • You can use indexed placeholders {0}, {1}, etc., for ordering.
  • Named placeholders can also be used for clarity: {“name”: name}.

3. Using f-Strings (Python 3.6+)

f-strings offer a modern way to format strings, allowing for inline expressions and variable references directly within the string.

name = 'Bob'
salary = 75000
years = 5

formatted_string = f"{name} earns ${salary} per year and has been working for {years} years."
print(formatted_string)

Output:

Bob earns $75000 per year and has been working for 5 years.

Advantages:

  • Readable and concise.
  • Supports expressions directly inside the curly braces.

Conclusion

Strings are a fundamental part of Python, offering rich functionality for manipulation and processing. With various built-in methods, Python strings provide the tools needed for formatting, slicing, splitting, and transforming text data, making them essential in most applications.

Knowledge Check

Related Article No.4

Mastering String Data Type: A Comprehensive Guide
Data Analytics with Power Bi and Fabric
Could Data Engineer
Data Analytics With Power Bi Fabic
AWS Data Engineering with Snowflake
Azure Data Engineering
Azure & Fabric for Power bi
Full Stack Power Bi
Subscribe to our channel & Don’t miss any update on trending technologies

Kick Start Your Career With Our Data Job

Master Fullstack Power BI – SQL, Power BI, Azure Cloud & Fabric Tools
Master in Data Science With Generative AI Transform Data into Business Solutions
Master Azure Data Engineering – Build Scalable Solutions for Big Data
Master AWS Data Engineering with Snowflake: Build Scalable Data Solutions
Transform Your Productivity With Low Code Technology: Master the Microsoft Power Platform

Social Media channels

► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *