✨PYTHON SERIES FOR HACKER: 4.1

BEGINNER TO ADVANCED

Agape HearTs
3 min readDec 10, 2023

IN THIS BLOG WE GONNA DISCUSS ABOUT :

  • String part 1

In Python, a string is a sequence of characters, and it is one of the basic data types provided by the language. Strings are used to represent text data and are enclosed in either single quotes (') or double quotes ("). For example:

Both single and double quotes can be used interchangeably to define strings, but you should use the same type of quote to start and end a string. If you need to include a quote character within the string itself, you can use the other type of quote or use an escape character \. Here's an example:

using other type of quote
using escape character

Python strings are immutable, meaning that once a string is created, you cannot change the characters it contains. However, you can create new strings by performing operations on existing ones.

We can inject values repeatedly in a variable in two methods frist one is like this:

or like this:

python has inbuilt function to find length of string: len()

The “in” built-in function is used to check if a value is present in a sequence, and it returns a Boolean value (True or False);

The startswith() method in Python checks if a string begins with a specified prefix and returns True if it does.

startswith()

index() is used for finding the position of a substring within a string.

index()

Python provides numerous built-in methods for working with strings, such as upper(), lower(), strip(), replace(), find(), split(), etc.

my_string = “ Hello, World! “
print(my_string.strip()) # Remove leading and trailing whitespaces
print(my_string.lower()) # Convert to lowercase
print(my_string.replace(‘Hello’, ‘Hi’)) # Replace substring

--

--