String Methods

There are three different types of strings in python. They are

string literals
raw strings
formatted strings

String literals – String literals can be defined using single quotes ‘, double quotes ” and triple quotes “””. The escape characters are m…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Saravanan

There are three different types of strings in python. They are

  • string literals
  • raw strings
  • formatted strings

String literals - String literals can be defined using single quotes ', double quotes " and triple quotes """. The escape characters are mentioned inside using a backslash \ for example a new line would be \n.

Raw strings - In raw string a backslash is treated as a literal character. A string is treated as a raw string of its prefixed with an r in front. Raw strings are used for regular expressions.

Formatted strings - Also called f-strings allows us to insert values of variables into a string. The value of the variable or expression inside curly braces will replace the curly brace section inside the f-string. Placing the semicolon : after the variable inside the curly brace will help change the format specification.

String concatenation

String concatenation can be done by both the + operator and the join function and f-string. In terms of performance, the f-string is the fastest one, followed by the join and plus operators the last.

Common String methods

capitalize() - capitalizes the first character of a string and all others in lowercase.

"first Second".capitalize() # returns "First second"

center(width[, fillchar]) - returns a string in the center within a specified width.

"word".center(10, "-") # returns "---word---"

count(sub[, start[, end]]) - returns the number of occurrences of a substring within a string.

"hello".count("l") # returns 2

find(sub[, start[, end]]) - returns the lowest index of the substring within the string.

"hello world".find("o") # returns 4

format(*args, **kwargs) - performs string formatting operation.

"Hi {}, It's {} now.".format("name", 1 + 1) # returns "Hi name, It's 2 now."

format_map(mapping) - formats a string using a dictionary.

"Hi {name}, It's {time} now.".format_map({"name": "Batman", "time": 12}) # returns "Hi Batman, It's 12 now."

index(sub[, start[, end]]) - returns the lowest index of the substring within the string.

"hello".index("o") # returns 4

isalnum() - returns True if all characters in the string are alphanumeric.

"a1".isalnum() # returns True

isalpha() - returns True if all characters in the string are alphabetic.

"hello".isalpha() # returns True

isdecimal() - returns True if all characters in the string are decimal. Decimal characters are those that can be used to form numbers in base 10.

 "123".isdecimal() # returns True

isdigit() - returns True if all characters in the string are digits. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits.

"¹²³".isdigit() # returns True

isnumeric() - returns True if all characters in the string are numeric. Numeric characters include digit characters, and all characters that have the Unicode numeric value property.

"ⅠⅩⅤⅬⅭⅮⅯ".isnumeric() # returns True

islower() - returns True if all characters in the string are lowercase.

"hello".islower() # returns True

isupper() - returns True if all characters in the string are uppercase.

"HELLO".isupper() # returns True

join(iterable) - returns a string concatenated with the elements of an iterable.

" ".join(["Hello", "World"]) # returns "Hello World"

lower() - converts the string to lowercase.

"Hello".lower() # returns "hello"

replace(old, new[, count]) - returns a string with all occurrences of the old string replaced with the new string.

"Hi First".replace("First", "New") # returns "Hello New"

split([sep[, maxsplit]]) - returns a list of the words in the string.

"Hello World".split() # returns ["Hello", "World"]

strip() - returns string with leading and trailing whitespace removed.

"hello  ".strip() # returns "hello"

upper() - converts the string to uppercase.

"hello".upper() # returns "HELLO"


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Saravanan


Print Share Comment Cite Upload Translate Updates
APA

Saravanan | Sciencx (2023-02-16T04:27:34+00:00) String Methods. Retrieved from https://www.scien.cx/2023/02/16/string-methods/

MLA
" » String Methods." Saravanan | Sciencx - Thursday February 16, 2023, https://www.scien.cx/2023/02/16/string-methods/
HARVARD
Saravanan | Sciencx Thursday February 16, 2023 » String Methods., viewed ,<https://www.scien.cx/2023/02/16/string-methods/>
VANCOUVER
Saravanan | Sciencx - » String Methods. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/16/string-methods/
CHICAGO
" » String Methods." Saravanan | Sciencx - Accessed . https://www.scien.cx/2023/02/16/string-methods/
IEEE
" » String Methods." Saravanan | Sciencx [Online]. Available: https://www.scien.cx/2023/02/16/string-methods/. [Accessed: ]
rf:citation
» String Methods | Saravanan | Sciencx | https://www.scien.cx/2023/02/16/string-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.