Python Strings Example Tutorial

In this post, you will learn everything about python strings. and also learn how to create, format, modify strings in Python.

If you want to manipulate with strings in python. So Many Python methods available, such as replace() , join() , or split() modify strings. However, they do not modify the original string.

Strings in Python

And in this python string tutorial, we will tell you how to use the python in-built function with strings. and some important python in-build methods, which are used with python strings.

What is String in Python?

In python, A string is a sequence of characters. Strings are immutable. 

How to create a string in Python?

To create a string in python, enclose the characters in Python with a single quote or double-quotes. And triple quotes are used to create multiline strings.

Let us take an example in which we will make string using single quote, double-quotes and triple quotes. You can see the example below:

# all of the following are same
my_string = 'Hello'
print(my_string)

my_string = "Hello"
print(my_string)

my_string = '''Hello'''
print(my_string)

Output

Hello
Hello
Hello

Python create multiline string

So far we have created a single-line string. Now we will learn how to create multiple line strings in Python.

For this, we put an example in which we will make a multiline string by using triple quote. The example can be seen below:

# triple quotes string can extend multiple lines
my_string = """Hello, welcome to
           the world of Python"""
print(my_string)

Output

      Hello, welcome to
            the world of Python

Python access character in string

From the above-given example, we learned how to create a string. Now we will learn how to access character from the given string in python.

For this, we take a simple example:

str = 'python'

#access character from string
print('str[0] = ', str[0])

Output

str[0] =  p

String slice python

String slice in python means accessing the characters from a given string in range.

You can access characters in range using the slice syntax.

Now we start an example below. So that you will get a better understanding of how to get characters in a range from a string:

str = "Hello, World!"
print(str[2:5])

Output

llo

With the example given above, you must have understood what string slicking is in Python.

Python string built-in methods

Now we will expose you about some in-built string methods in Python.

Python string methods are used to manipulate string, we will understand some of Python’s in-built methods with example.

We will understand how to use in-built methods of these pythons with string.

Python string length

Python has a len() method and this is in-built method. We use this when we have to find the length of the given string in python.

Let’s take an example and understand how this python len () method works:

str = "Hello, World!"
print(len(str))

Output

13

Strip string python

Let’s understand another in-built method of Python. Which is strip() method.

In Python, strip() method is used with string. Because this method removes whitespace from the beginning and end of string in python.

Many times you need to remove the whitespace from the start and end of the string in python. Checkout the example below:

str = "  Hello, World!  "
print(str.strip())

Output

Hello, World!

You can see that we have taken an example. It has a lot of whitespace in the start and end of the string. But when we used strip (), that whitespace was removed.

String to uppercase python

Let’s take the other in-built method of Python. Which is upper() method.

Python upper () method, which is used to convert string characters and letters to uppercase.

If you want to convert all the letters and characters of the string to upper case, you can use the python upper() method.

Convert all lowercase letters to uppercase letters in python:

str = "hello, world!"
print(str.upper())

Output

 HELLO, WORLD!

As you can see in the example. We declared a variable and called this variable “hello world!” Assign string | These were all in lowercase.

The variable in which the string was placed in lower case. Used Python’s upper () method with it. And with this method all the strings were converted to uppercase.

Python string to lowercase

Here, we will understand the python lower() method, which is a built-in method in python.

Python lower() method, which is used to convert string characters and letters to lowercase.

If you want to convert all the letters and characters of the string to lowercase, you can use the python lower() method.

string to lower python:

str = "HELLO, WORLD!"
print(str.lower())

Output

 hello, world!

As you can see in the example. We declared a variable and called this variable “HELLO, WORLD!” Assign string | These were all in uppercase.

The variable in which the string was placed in uppercase. Used Python’s lower() method with it. And with this method, all the strings were converted to lowercase.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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