A string is a series of characters. In Python, anything inside quotes is a string. And you can use either single or double quotes.
The Python interpreter will treat the backslash character (\) special. If you want to print (\) then use r before string.
For multiple lines string you use triple-quotes “””…””” or ”’…”’.
you want to use the values of variables in a string then you write the f before the opening quotation mark. write variable in { }.
When you place the string literals next to each other, Python automatically concatenates them into one string.
+ operator is used to concatenate multiple string variable.
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.
len() function is used to get length of 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]
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.
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.
Comsysapp.com provides free tutorials like c, html, css, etc. All tutorials are free for beginner and professionals.
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.
comsysapp.com does not collect any data from users. We use Google AdSense advertising on our website. We never ask personal or private information.
copyright © 2023