Python Tutorial
About Lesson

In this Python Tutorial we want to learn about Python Tutorial – Python String, because most programs gather some sort of data, and after that they do something useful with that. now first of all what is string ? in Python string is sequence of characters. it is used to represent text data such as words, sentences,and paragraphs. Strings can be created using single quotes (‘…’) or double quotes (“…”).

 

In this example we have our two string. The string is created using double quotes and contains the text “Hello World!”.

Strings in Python are immutable which means that once string is created, it cannot be changed. However you can create new strings based on existing strings by using string manipulation techniques like slicing, concatenation and formatting.

 

 

Also we can use variables with Strings.

 

 

String Indexing and Slicing: you can access individual characters in string using indexing and slices. Indexing starts at 0 and goes up to len(string) – 1. Slicing can be used to access a range of characters in a string. for example:

 

 

 

Run the code and this will be the result.

Python Tutorial - Python String
Python Tutorial – Python String

 

 

String Methods: Python provides different built in string methods that you can use to manipulate and analyze strings. some examples include:

 

 

Run the code and this will be the result.

Python Tutorial - Python String
Python Tutorial – Python String

 

 

 

Also you can use f-string, so F-strings provide a way to embed expressions inside string literals, using a minimal syntax.

 

 

 F-String was introduced in python 3.6, if you are using python 3.5 than you can use .format.

 

 

 

You can add newline and tab in the string 

 

 

 

This is the result.

Python String Data Type
Python String Data Type

 

 

 

Also you can concatenate two strings, for example you have two strings and you want to make it one string than you can simply add these two strings.

 

 

 

Also you can do slicing on python strings, for example in here i want to get the characters from 2 up to 4.

 

 

Regular Expressions: Regular expressions (or “regex”) are powerful tool for working with text data. Python’s re module provides support for regular expressions. for example:

These are just few examples of the many things you can do with Python strings. by mastering the basics and exploring more advanced features, you can work effectively with text data in your Python programs.