String in python

Python string introduction

A string is a series of characters. In Python, anything inside quotes is a string. And you can use either single or double quotes.

See Output

The Python interpreter will treat the backslash character (\) special. If you want to print (\) then use r before string.


Creating multiline strings

For multiple lines string you use triple-quotes “””…””” or ”’…”’.

See Output

Python strings with the f-strings

you want to use the values of variables in a string then you write the f before the opening quotation mark. write variable in { }.

See Output

Concatenating Python strings

When you place the string literals next to each other, Python automatically concatenates them into one string.

See Output

Concatenating Python Variables

+ operator is used to concatenate multiple string variable.

See Output

Accessing string elements

Since a string is a sequence of characters, you can access its elements using an index. The first character in the string has an index of zero.

access the first and second characters of the string by using the square brackets [] and indexes.

If you use a negative index, Python returns the character starting from the end of the string.

See Output

Getting the length of a string

len() function is used to get length of string.

See Output

How to get substring from a string

start and end are index of string.

The start and end are optional. If you omit the start, it defaults to zero. If you omit the end, it defaults to the string’s length.

Syntax

string[start:end]

See Output

Python Raw String

In some case you have need to print backslash (\). Suppose a string already save with backslash (\) in database and you want to print.
Example1: C:\docs\pythonprogram\tutorial
Example2: C:\docs\pytho\nprogram\tutorial In example2 you can see new line (\n) character and tab space(\t) character.

In python \n means new line. \n used to print string from new line. \t means one tab space. If you want to print one tab space then use \t in python programme.

If you want to print all character with backslash (\) like \n and \t then use raw string (r) before the first quote.

See Output

About Us

Comsysapp.com is an educational website. Students and software developers can learn programming language tutorials. Comsysapp is very useful for beginners and professional developers. Comsysapp provides tutorial in easy language. Comsysapp.com has focus on simplicity.

Services

Comsysapp.com provides free tutorials like c, html, css, etc. All tutorials are free for beginner and professionals.


Terms of Use

comsysapp.com is not responsible for any mistake. We are not responsible if information made available on our website is incomplete or invalid. But comsysapp.com always try for zero-zero mistake.

Privacy policy

comsysapp.com does not collect any data from users. We use Google AdSense advertising on our website. We never ask personal or private information.


Sitemap

sitemap

copyright © 2023