DEV Community

SATHISHKUMAR R
SATHISHKUMAR R

Posted on

What is String in Python?

A String in Python is a collection of characters (letters, numbers, symbols, or spaces) enclosed within quotes.

We can use single quotes (' '), double quotes (" "), or triple quotes (''' ''').

  1. Example of String:

name = "Sathish"
city = 'Chennai'
message = "Hello World"
print(name)
print(city)
print(message)

Output:
Sathish
Chennai
Hello World

  1. Types of String Quotes:

Single quotesname = 'Sathish'# Double quotescity = "Chennai"# Triple quotesmessage = """Welcome to Python"""

  1. String Operations:

  2. Concatenation (Joining strings):

first = "Hello"
second = "World"
print(first + " " + second)

Output:
Hello World

  1. Repetition:

name = "Hi"
print(name * 3)

Output:
Hi Hi Hi

  1. Length: name = "Sathish" print(len(name))

Output:
7

  1. Accessing characters (Indexing):

name = "Python"
print(name[0])
print(name[1])

Output:
P
y

  1. Important String Methods: Method Purpose upper() Converts to uppercase lower() Converts to lowercase capitalize() Capitalizes first character replace() Replaces characters strip() Removes leading/trailing spaces split() Splits a string into a list find() Finds a substring

Example:
name = "sathish"
print(name.upper())
print(name.capitalize())

Output:
SATHISH
Sathish

Top comments (0)