Strings in Python
What are the strings ?
String properties
Python strings can be enclosed in single, double or triple quotes.
print('Hello World')
print("Hello World")
print('''Hello World''')
print("""Hello World""")
//Output
Hello World
Hello World
Hello World
Hello World
Python strings are immutable
Example:
x = "Hello World"
x = "Hello"
x[0] = M
print(x)
//Output
TypeError: 'str' object does not support item assignment
String concatenation
first_name = "Elon"
last_name = "-Musk"
full_name = first_name + last_name
print(full_name)
//Output
Elon-Musk
String slicing
The syntax for string slicing in Python is string[start: end: step]
Here, are some examples of string slicing in Python
string = "hello"
char = string[1]
print(char)
//Output
"e"
2. Extracting a substring: string = "hello world"
substring = string[7:12]
print(substring)
//Output
"world"
- Extracting a substring from the end of the string:
string = "hello world"
substring = string[-6:]
print(substring)
//Output
"world"
- Extracting a substring from the start of the string:
string = "hello world"
substring = string[:5]
print(substring)
//Output
"hello
- Extracting every nth character:
string = "hello world"
substring = string[::2]
print(substring)
//Output
"hlowrd"
- Extracting a substring in reverse order:
string = "hello world"
substring = string[::-1]
print(substring)
//Output
"dlrow olleh"
- Extracting a substring using both positive and negative indexing:
string = "hello world"
substring = string[-6:5]
print(substring)
//Output
"wor"
Strings Methods
Method | Description |
Converts the first character to upper case | |
Converts string into lower case | |
Returns a centered string | |
Returns the number of times a specified value occurs in a string | |
Returns an encoded version of the string | |
Returns true if the string ends with the specified value | |
Sets the tab size of the string | |
Searches the string for a specified value and returns the position of where it was found | |
Formats specified values in a string | |
format_map() | Formats specified values in a string |
Searches the string for a specified value and returns the position of where it was found | |
Returns True if all characters in the string are alphanumeric | |
Returns True if all characters in the string are in the alphabet | |
Returns True if all characters in the string are ascii characters | |
Returns True if all characters in the string are decimals | |
Returns True if all characters in the string are digits | |
Returns True if the string is an identifier | |
Returns True if all characters in the string are lower case | |
Returns True if all characters in the string are numeric | |
Returns True if all characters in the string are printable | |
Returns True if all characters in the string are whitespaces | |
Returns True if the string follows the rules of a title | |
Returns True if all characters in the string are upper case | |
Converts the elements of an iterable into a string | |
Returns a left justified version of the string | |
Converts a string into lower case | |
Returns a left trim version of the string | |
Returns a translation table to be used in translations | |
Returns a tuple where the string is parted into three parts | |
Returns a string where a specified value is replaced with a specified value | |
Searches the string for a specified value and returns the last position of where it was found | |
Searches the string for a specified value and returns the last position of where it was found | |
Returns a right justified version of the string | |
Returns a tuple where the string is parted into three parts | |
Splits the string at the specified separator, and returns a list | |
Returns a right trim version of the string | |
Splits the string at the specified separator, and returns a list | |
Splits the string at line breaks and returns a list | |
Returns true if the string starts with the specified value | |
Returns a trimmed version of the string | |
Swaps cases, lower case becomes upper case and vice versa | |
Converts the first character of each word to upper case | |
Returns a translated string | |
Converts a string into upper case | |
Fills the string with a specified number of 0 values at the beginning |
Escape sequences
\n
: represents a newline character. It is used to insert a new line in a string.
string = "Hello\nWorld"
print(string)
//Output:
Hello
World
\t
: represents a tab character. It is used to insert a tab space in a string.
string = "Hello\tWorld"
print(string)
//Output:
Hello World
\"
: represents a double quote character. It is used to include a double quote within a string that is enclosed in double quotes.
string = "He said, \"Hello World\""
print(string)
//Output:
He said, "Hello World"
4. '\\': represents a backslash. It is used to include a backslash within a string.
string = "Hello\\World"
print(string)
//Output:
Hello\World
5.: represents a Carriage return. It is used to move the cursor to the beginning of the current line.
string = "Hello\rWorld"
print(string)
//Output:
Worldllo
These are just a few examples of escape sequences in Python. There are other escape sequences available as well, such as '\a'
,'\b'
,'\f'
,'\v'
, etc. Each escape sequence has a specific use case, and it's good to familiarize yourself with them to be able to use the appropriate one for a specific task.
No comments:
Post a Comment