Python Strings | Basics of Python - Software Development PDF Download


In Python, strings are an essential data type used to represent text. They are sequences of characters enclosed in either single quotes (' ') or double quotes (" "). In this article, we will explore the basics of strings in Python, including creating and manipulating strings, accessing individual characters, string concatenation, and various string methods.

Creating Strings

To create a string in Python, you can simply assign a sequence of characters to a variable. Here are a few examples:

name = "Alice"

message = 'Hello, World!'

empty_string = ""

Accessing Characters in a String

You can access individual characters within a string by using the index. In Python, indexing starts from 0, meaning the first character has an index of 0. Here's an example:

name = "Alice"

print(name[0])  # Output: A

print(name[2])  # Output: i

String Slicing

String slicing allows you to extract a portion of a string. It is done by specifying the starting and ending indices within square brackets. The ending index is exclusive. Let's see an example:

message = "Hello, World!"

print(message[0:5])  # Output: Hello

print(message[7:])   # Output: World!

String Concatenation

String concatenation is the process of combining two or more strings. In Python, you can concatenate strings using the + operator. Here's an example:

first_name = "John"

last_name = "Doe"

full_name = first_name + " " + last_name

print(full_name)  # Output: John Doe

String Length

To determine the length of a string (i.e., the number of characters), you can use the len() function. Here's an example:

message = "Hello, World!"

print(len(message))  # Output: 13

String Methods

Python provides a variety of built-in methods to manipulate strings. Here are a few commonly used methods:

  • 'lower()' and 'upper()': Convert a string to lowercase or uppercase, respectively.
  • 'strip()': Remove leading and trailing whitespace from a string.
  • 'split()': Split a string into a list of substrings based on a specified delimiter.
  • 'replace()': Replace occurrences of a substring with another substring.

Let's see these methods in action:

message = "   Hello, World!   "

print(message.lower())        # Output: hello, world!

print(message.strip())        # Output: Hello, World!

print(message.split(','))     # Output: ['   Hello', ' World!   ']

print(message.replace('o', 'a'))  # Output:   Hella, Warld!   

Sample Problems

Problems 1: Write a program that asks the user to enter their name and prints a welcome message with the name capitalized.

name = input("Enter your name: ")

print("Welcome, " + name.capitalize() + "!")

Problems 2: Write a program that counts the number of occurrences of a specific character in a given string.

def count_char(string, char):

    count = 0

    for c in string:

        if c == char:

            count += 1

    return count

message = "Hello, World!"

print(count_char(message, 'l'))  # Output: 3


In this article, we covered the basics of strings in Python. We learned how to create strings, access individual characters, perform string slicing, concatenate strings, determine the length of a string, and use some useful string methods. By understanding these concepts, you'll be well-equipped to work with strings and handle various string operations in Python.

The document Python Strings | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development
49 videos|38 docs|18 tests
49 videos|38 docs|18 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches



past year papers


Python Strings | Basics of Python - Software Development


Sample Paper


practice quizzes


mock tests for examination


study material


Objective type Questions


Python Strings | Basics of Python - Software Development


Python Strings | Basics of Python - Software Development




video lectures


Semester Notes




shortcuts and tricks






Viva Questions




Previous Year Questions with Solutions


Important questions


Extra Questions