在python

时间:2018-05-04 14:41:14

标签: python string python-3.x

我是python的新手。我以前用过c。在那里我们可以使用字符串中的索引逐个字符地工作。我试图将整个字符串转换为小写字母,将其反转并使用大写字母

中的第一个字符进行打印
first_name = input("enter your first name\n")
last_name = input("enter your last name\n")
first_name = first_name.lower()
last_name = last_name.lower()
def rev(s):
 str = ""
 for i in s:
    str = i + str
    return str
first_name = rev(first_name)
last_name = rev(last_name)
first_name[0].upper()
last_name[0].upper()
print(first_name + " " + last_name)

输出

输入您的名字

Manash

输入您的姓氏

夏尔马

m s

2 个答案:

答案 0 :(得分:0)

  • 您可以使用[::-1]来反转字符串
  • str.capitalizestr.title获取首字母大写

<强>演示:

first_name = input("enter your first name\n")
last_name = input("enter your last name\n")
first_name = first_name[::-1].capitalize()
last_name = last_name[::-1].capitalize()

答案 1 :(得分:0)

你的错误在于直接在for循环中返回,并且没有分配大小写:

** 
    call_once_xcp.cpp

    Demonstrate that if the first call to the call_once() function
    is unsuccessful, it will invoke a subsequent functionality.

**/

#include <mutex>        /// once_flag, call_once()
#include <thread>       /// thread
#include <exception>        /// runtime_error
#include <iostream>     /// cout

using namespace std;

once_flag of;

/// declarations ...
void func_call_xcp();   /// will call func_xcp()
void func_xcp();    /// will throw
void func_call_OK();    /// will call func_OK()
void func_OK();     /// won't throw


int main()
{
   thread t1 {func_call_xcp};
   t1.join();

   thread t2 {func_call_OK};
   t2.join();

   thread t3 {func_call_OK};
   t3.join();
}


/// will call func_xcp()
void func_call_xcp()
{
   try
   {
      call_once(of, func_xcp);
   }
   catch (exception& e)
   {
      cout << "exception: " << e.what()
           << endl;
   }
}


/// will call func_OK()
void func_call_OK()
{
   call_once(of, func_OK);
}


void func_xcp()     /// will throw
{
   cout << "** func_xcp()" << endl;

   throw runtime_error 
       {"error in func_xcp()"};
}


void func_OK()      /// won't throw
{
   cout << "** func_OK()" << endl;
}

请注意,当然有更好,更快,更简单的方法,如另一个答案所示。