Python get letter in string

Python: Get the First Character of String

Here, we will develop a Python program to get the first character of a string. If the string was “Knowprogram” then print the first character “K”. We will discuss how to get the first character from the given string using native methods, and slice operator. Also, we will develop a Python program to get the first two characters of a string.

Get First Character of String in Python

We will take a string while declaring the variables. Then, we will run the loop from 0 to 1 and append the string into the empty string (first_char). Finally, the first character will be displayed on the screen.

# Python Program get first character of string # take input string = input('Enter any string: ') # get first character first_char = "" for i in range(0, 1): first_char = first_char + string[i] # printing first character of string print('First character:', first_char)

Output for the different input values:-

Enter any string: Python
First character: P

Enter any string: Know Program
First character: K

Python Program for First Character of String

In python, String provides an [] operator to access any character in the string by index position. We need to pass the index position in the square brackets, and it will return the character at that index. As indexing of characters in a string starts from 0, So to get the first character of the given string pass the index position 0 in the [] operator i.e.

# Python Program get first character of string # take input string = input('Enter any string: ') # get first character first_char = string[0] # printing first character of string print('First character:', first_char)

Enter any string: character
First character: c

Читайте также:  Внешняя таблица стилей

Get First Character of String using Slicing

We will get the first character of the string using the slice operator. The [:1] specifies the character at index 0. The string[:1] specifies the first characters of the given string.

# Python Program get first character of string # take input string = input('Enter any string: ') # get first character first_char = string[:1] # printing first character of string print('First character:', first_char)

Источник

Get first letter of every word in String in Python

In this article, we will discuss how to get first letter of every word in String in Python.

Table Of Contents

Introduction

Suppose we have a string, like this,

"This is a big string to test."

We want to create a list, containing the first letter of every word in the String. Like this,

There are different ways to do this. Let’s discuss them one by one.

Frequently Asked:

Method 1: Using For Loop

Follow these steps to get first letter of each word in a string,

  • Create an empty list to hold the first letter of each word of string.
  • Split the string into a list of words using the split() method of string class.
  • Iterate over each word in the list, using a for loop, and for each word, append its first letter to the list created in first point.
  • Now, this new list will have the first letter of each word from the string.
strValue = "This is a big string to test." # Split string into a list of words listOfWords = strValue.split() # List to contain first letter of each word listOfLetters = [] # Iterate over all words in list for word in listOfWords: # Select first letter of each word, and append to list listOfLetters.append(word[0]) print(listOfLetters)

We create a list, and filled it with the first letter of every word from the string.

Читайте также:  Ремонт компьютеров шаблоны html

Method 2: Using List Comprehension

Split the string into a list of word using split() method of string class. Then iterate over all words in the list, and from each word select the first letter. Do, all this inside the List Comprehension, and build a list of first letters of each word. Let’s see an example,

strValue = "This is a big string to test." # Select first letter of each word, and append to list listOfLetters = [ word[0] for word in strValue.split()] print(listOfLetters)

We create a list, and filled it with the first letter of every word from the string.

Summary

We learned about two different ways to fetch the first letter of each word in a string in Python. Thanks.

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Читайте также:  Помощь все о php

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Оцените статью