Python Tutorial String Variable

String variables are a sequence of characters. String type can store words, sentences, and paragraphs. they are one of the most important types of variables in all programming languages.

How to assign string to variable in Python?

We write the name of the variable and we write this sign (=) then we write our text inside single or double-quotes.

Syntax:

#____ProgrammerCodes.com____
variable_name = "Write your text here"
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
message= "Thank you";
print(message)
#____ProgrammerCodes.com____

Result

Thank you

What are string methods in python?

Python has so many built-in methods that allow you to manipulate strings.

How to use capitalize() function in python?

Try this method If you want the first character of a string to be capitalized,

Syntax:

#____ProgrammerCodes.com____
var.capitalize();
#____ProgrammerCodes.com____

var is a variable that contains a string.

Example:

#____ProgrammerCodes.com____
name = "adam";
print(name.capitalize())
#____ProgrammerCodes.com____

Result

Adam

Video

How to use len() function in Python?

This method allows you to get the length of a string.

Syntax:

len(var)

Example:

#____ProgrammerCodes.com____
city = "Manchester";
print(len(city));
#____ProgrammerCodes.com____

Result

10

How to use lower() function in Python?

This method will convert any uppercase letters to lowercase.

Syntax:

#____ProgrammerCodes.com____
var.lower();
#____ProgrammerCodes.com____

Example:

#____ProgrammerCodes.com____
message = "This is PROgrammerCODEs bLOG";
print(message.lower());
#____ProgrammerCodes.com____

Result

this is programmercodes blog

How to use upper() function in Python?

This method will convert any lowercase letters to uppercase.

Syntax:

var.upper();

Example:

#____ProgrammerCodes.com____
message = "This is programmercoDes bloG";
print(message.upper());
#____ProgrammerCodes.com____

Result

THIS IS PROGRAMMERCODES BLOG

Tutorial’s Question

What is the role of upper() method?

  • Capitalize the words of a string variable.
  • Turn a string into tuple.
  • Convert lowercase letters to uppercase.

Simple Examples

If you have learned the basics of Python and you want to see some simple examples about these concepts such as numbers, for loop, function, and so on, I suggest you visit these articles below:

Leave a Comment

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